public class A { //some fields (may not only primitive types) and methods here}
public class B {// some fields (may not only primitive types) and methods here, may also have a class A reference }
Вопросы следуют:
public class Test{
public static void main(String[] args){
A a = new A();//1. it will allocate memory for one object of A and a is the reference points to that space?
ArrayList<B> bList = new ArrayList<B>(10);//2. it will allocate memory for 10 objects of B?
ArrayList<B> bList2 = bList;//3. bList2 reference to the same location as bList?
ArrayList<B> bList3 = new ArrayList<B>(20);//4. bList3 points to a memory location which can hold 20 objects of B?
bList3 = bList;//5. bList3 now points to the same location as bList, and the space allocated in question 4 will be cleaned by garbage collector later?
ArrayList<B> bList4 = new ArrayList<B>(10);
bList4.addAll(bList);//6. it is the same as bList4 = bList;? and the memory created in above line will be cleaned by garbage collector later?
method1(bList3);//7.after this function call bList3 will point to memory space created for bLista inside the method1? and we can modify the content of that space via bList3
}
public void method1(ArrayList<B> list){
//do something here
ArrayList<B> bLista = new ArrayList<B>();
list = bLista;
}
}