62,621
社区成员
发帖
与我相关
我的任务
分享
import java.util.*;
public class TestAtuoBox{
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
Collections.addAll(list,1,2,3);
System.out.println(list);
/////////////////////////////////////////////////////
Integer num1 = new Integer(1);// 不同于Integer num1 = 1; 我的这句涉及到常量池取值以及自动打包
Integer num2 = num1;
System.out.println(num1 == num2);
// 从JDK5.0开始 基本数据类型和包装类类型之间可以直接赋值[自动打包 自动解包]
// 下面的这行代码创建了一个新变量,原因如下
num2 = 2; // 等同于执行了 num1 = Integer.valueOf(2); 自动打包[@since JDK 5.0]
System.out.println("num1是:" + num1 + " num2是:" + num2);
/////////////////////////////////////////////////////
System.out.println("------------------------------");
for(Iterator<Integer> car = list.iterator(); car.hasNext(); ){
Integer a = car.next(); // 相当于上面的 Integer num2 = num1;
a = 123; // 相当于上面的 num2 = 2;
// 没明白下面是什么意思。如果想遍历List集合,不如用foreach或迭代器
System.out.println(list.get(0));
}
}
}