List<String> list =Arrays.asList("apple","banana","orange");for (String fruit : list) {System.out.println(fruit);}
等价于:
List<String> list =Arrays.asList("apple","banana","orange");Iterator<String> iterator =list.iterator();while (iterator.hasNext()) {String fruit =iterator.next();System.out.println(fruit);}
b) 自动装箱和拆箱
自动装箱和拆箱使得基本数据类型与其对应的包装类之间可以自动转换。
int num =10;Integer number = num; // 自动装箱int result = number +5; // 自动拆箱
等价于:
int num =10;Integer number =Integer.valueOf(num); // 手动装箱int result =number.intValue() +5; // 手动拆箱