2 回答
TA贡献1846条经验 获得超7个赞
这就是后台发生的事情。我会尝试将其形象化,以便于理解。
创建整数对象:
Integer num = new Integer(11);
num --> @num493857 (some reference not real of course)
@num493857 --> ------
| 11 |
------
创建数组列表:
List<Integer> arr = new ArrayList<>();
arr --> @arr7686
@arr7686 --> [] (empty list)
将 num 添加到 Arraylist:
arr.add(num);
arr --> @arr7686 --> [@num493857] // @num493857 has 11 in it
^
|
num
设置 arr2 = arr:
List arr2 = arr;
arr --> @arr7686 --> [@num493857]
^ ^
| |
arr2 num
设置 arr = null:
arr = null
arr --> null
@arr7686 --> [@num493857]
^ ^
| |
arr2 num
打印arr2:
System.out.println(arr2);
This prints whatever is in the list that is 11.
添加回答
举报