自动拆箱: int i = new Integer(6);,底层调用i.intValue();方法实现。
Integer i = 6; Integer j = 6; System.out.println(i==j);
答案在下面这段代码中找:
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
public class Test { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(2); Iterator<Integer> iterator = list.iterator(); while(iterator.hasNext()){ Integer integer = iterator.next(); if(integer==2) list.remove(integer); } } }
HashMap可以通过下面的语句进行同步: Map m = Collections.synchronizeMap(hashMap);
11. 拷贝文件的工具类使用字节流还是字符流
答案:字节流
11.1 什么是字节流,什么是字符流?
字节流:传递的是字节(二进制),
字符流:传递的是字符
11.2 答案
我们并不支持下载的文件有没有包含字节流(图片、影像、音源),所以考虑到通用性,我们会用字节流。
12. 线程创建方式
这个之前自己做过总结,也算比较全面。
方法一:继承Thread类,作为线程对象存在(继承Thread对象)
public class CreatThreadDemo1 extends Thread{ /** * 构造方法: 继承父类方法的Thread(String name);方法 * @param name */ public CreatThreadDemo1(String name){ super(name); }
@Override public void run() { while (!interrupted()){ System.out.println(getName()+'线程执行了...'); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } }
public static void main(String[] args) { CreatThreadDemo1 d1 = new CreatThreadDemo1('first'); CreatThreadDemo1 d2 = new CreatThreadDemo1('second');
public class CreatThreadDemo4 implements Callable { public static void main(String[] args) throws ExecutionException, InterruptedException { CreatThreadDemo4 demo4 = new CreatThreadDemo4();
FutureTask<Integer> task = new FutureTask<Integer>(demo4); //FutureTask最终实现的是runnable接口
Thread thread = new Thread(task);
thread.start();
System.out.println('我可以在这里做点别的业务逻辑...因为FutureTask是提前完成任务'); //拿出线程执行的返回值 Integer result = task.get(); System.out.println('线程中运算的结果为:'+result); }
//重写Callable接口的call方法 @Override public Object call() throws Exception { int result = 1; System.out.println('业务逻辑计算中...'); Thread.sleep(3000); return result; } }
Callable接口介绍:
public interface Callable<V> { /** * Computes a result, or throws an exception if unable to do so. * * @return computed result * @throws Exception if unable to compute a result */ V call() throws Exception; }
返回指定泛型的call方法。然后调用FutureTask对象的get方法得道call方法的返回值。
方法五:定时器Timer
public class CreatThreadDemo5 {
public static void main(String[] args) { Timer timer = new Timer();