简体   繁体   English

Java:对可重定位对象的引用如何工作?

[英]Java: How does references to relocatable objects work?

By relocatable objects, I mean objects that can relocate itself in memory when there is not enough consecutive space. 可重定位的对象是指可以在没有足够连续空间的情况下将自身重新定位在内存中的对象。 (for example ArrayList) (例如ArrayList)

 List<String> list1 = new ArrayList<>();
 List<String> list2 = list1;

If I keep adding things to list1, it will eventually relocate because there is not enough continuous address space. 如果我继续将内容添加到list1,则它将最终重新定位,因为连续的地址空间不足。 Then will list2 and list still point to the same address or not? 然后,list2和list是否仍指向相同的地址? If so, how is it done underneath? 如果是这样,如何在下面完成?

No object can relocate itself. 没有对象可以自行重定位。 What happens here is that list1 and list2 point to the same object and this object (ie the ArrayList ) contains another reference to the actual storrage (ie the array that is used to store the data). 这里发生的是list1list2指向同一个对象,并且该对象(即ArrayList )包含对实际存储的另一个引用(即,用于存储数据的数组)。 If the list grows too big, the pointer to the storrage array changes, not the location of the ArrayList object itself. 如果列表太大,则指向存储数组的指针将更改,而不是ArrayList对象本身的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM