Reputation: 139
I running to a situation and I am very confused. Please help me out. Let's say I have a code like this.
MyClass obj1 = null;
List<MyClass> testList = new ArrayList<MyClass>();
testList.add(obj1);//after this line of code, testList will have a "null" at first node
obj1 = new MyClass();//after this line of code, testList still have a "null"
//at first node...
//What I want is that testList's first node will become a new
//MyClass object
Here is the steps in my understanding (probably incorrect...):
Sorry I am new to programming... Any help is appreciated!
Upvotes: 2
Views: 2259
Reputation: 51711
Java is strictly pass by value.
Much of your confusion comes from the fact that
testList.add(obj1); // makes you think that you've added a pointer to the list
and now if you modify the pointer as
obj1 = new MyClass(); // it would reflect at testList.get(0) as well.. NO!!
This would've been the case if Java were "pass by reference" but since it's not testList.add(obj1);
actually translates to testList.add(null);
copying the value and setting the first indexed position of the Array (backing this ArrayList) as null
.
Java doesn't work like C and C++ work with pointers. It was one of its design goals to simplify things by EDIT: removing pointers, their arithmetic, multiple inheritance, operator overloading etc. just to name a few.
To make it more clear if you modify your program as follows:
MyClass obj1 = new MyClass("Object 1"); // modify the constructor to take an id
List<MyClass> testList = new ArrayList<MyClass>();
testList.add(obj1);
obj1 = new MyClass("Object 2");
System.out.println(testList.get(0).toString()); // implement toSting() to id MyClass
testList
would contain "Object 1"
not "Object 2"
.
Upvotes: 0
Reputation: 116
When you add objects to ArrayList
objects will not be added to the ArrayList
but the pointers which point to the object will be added.
So if you do something like this:
Object obj1 = new Object() // [location: x1234]
list.add(obj1); // first index in list points to location x1234
obj1 = new Object(); // [location: x2345];
Now the array has pointer which is still pointing to the old location.
Same is the case with null
. Though you are changing the link of obj1
to new location the array still points to old location which is null
.
Upvotes: 1
Reputation: 26518
Here is the explination why testList still have a "null" at first node after these piece of code
testList.add(obj1);//after this line of code, testList will have a "null" at first node
obj1 = new MyClass();//after this line of code, testList still have a "null"
//at first node...
//What I want is that testList's first node will become a new
//MyClass object
Step 1
MyClass obj1 = null;
This line creates space for the MyClass reference variable (the bit holder for a reference value), but doesn't create an actual Myclass object.
Step 2
List<MyClass> testList = new ArrayList<MyClass>();
A list is created testList which can hold objects of MyClass types
Step 3
testList.add(obj1);//after this line of code, testList will have a "null" at first node
testList first node will now refer to an null But not an MyClass Object.
step 4
obj1 = new MyClass();
Creates a new MyClass object on the heap and Assigns the newly created MyClass object to the reference variable obj1.
So now how will the list gets updated it is still holding an null But not an MyClass Object.
So now if you want to make testList's first node to become a new MyClass object
Then write this lone of code after obj1 = new MyClass();
testList.set(0, obj1);
So now the full code after that would be
MyClass obj1 = null;
List<MyClass> testList = new ArrayList<MyClass>();
testList.add(obj1);//after this line of code, testList will have a "null" at first node
obj1 = new MyClass();
testList.set(0, obj1);
Thats all about your problem from my understanding.
Upvotes: 2
Reputation: 49352
MyClass obj1 = null;
A reference variable named obj1 of type MyClass
refers to nothing(NULL).
List<MyClass> testList = new ArrayList<MyClass>();
We declare a reference variable called testList of type List
and assign it to some newly created ArrayList
object in heap.
testList.add(obj1);
List
testList's first element is assigned the same reference which obj1 holds currently viz. NULL.
obj1 = new MyClass();
We created a new MyClass
object in heap and assigned obj1 with its reference. But we haven't assigned any new reference to the List
testList's first element, which was assigned the NULL reference, so it still points nowhere.
Upvotes: 1
Reputation: 129
Your list is still empty.
Upvotes: 0