Reputation: 313
I am trying to get more familiar with java recently. A question occurred to me which I couldn't find the answer online. I am wondering when java adds an element to an arraylist, is the added element associated with a variable name? For example in the following code:
E obj1 = new E();
E obj2 = new E();
List myList = new ArrayList<E>();
myList.add(obj1);
obj1 = obj2;
After the new assignment to obj1 will the value exist in myList change or not?
Upvotes: 0
Views: 428
Reputation: 30136
In Java, it's "all about references".
A new instance is created only when you use new
.
In all other assignments, such as obj1 = obj2
, you are only changing the reference to the actual instance.
The only exception for this rule are assignments that involve primitive types such as char
and int
.
When you do myList.add(obj1)
, you add an entry in myList
, which contains a reference to the instance referenced by obj1
.
At this point, this instance has two references (one by obj1
, and one by the entry in myList
).
When yo do obj1 = obj2
, the same instance remains with only one reference (in myList
).
When the number of references to an instance becomes 0, it is deleted by the garbage collector.
Upvotes: 2
Reputation:
obj1
contains a reference to an instance of the class E. When you 'add' the object to the list you are adding the reference to it so the list knows who is the new buddie.
When making obj1 = obj2
you are changing the value of obj1, not the instance itself, so that both variables point to the same instance of E.
Object variables are just a reference, so you are just changing the reference. Therefore, the instance you created with
E obj1 = new E();
is still alive and not changed at all.
By making an assignment of object variables you will never ever change the object itself but the reference.
Note: if an instance remains without being pointed by some variable the garbage collector of java will delete it. This is not the case as the instance is now pointed by the list.
Upvotes: 0
Reputation: 90
Before this question is answered the first thing to remember is that whenever you come across object is assigned to another object , you need to consider that reference is like remote control to the object. but in case object is assigned to another object , your reference(remote control) no longer controls your obj.
lets go line by line to see whats happening:
E obj1 = new E();
// the obj1 is created and holds reference to a memory location let's say 1a2s3d
E obj2 = new E();
// the obj2 is created and holds reference to say 5g5g5g5 .
List myList = new ArrayList<E>();
// empty list created acception elements of type E
myList.add(obj1);
//myList now points to reference 1a2s3d by addition of obj1 .
obj1 = obj2
//obj1 and obj2 now point to same reference .. 5g5g5g5
and myList still points to reference 1a2s3d because it points to reference not obj1.
So its value will not change.
Also any new changes to elements of obj1 or obj2 like setting value of any field in obj1 of class
E will change value of that field in obj2 also . thus the code is (obj1==obj2) ?? will return true.
Upvotes: 0
Reputation: 108
No ,in your case obj1 will not change in ArrayList because you already added obj1 to ArrayList before changing the obj1 to obj2.
Upvotes: 1
Reputation: 41188
Java is always pass-by-value. That means that the original variable before you pass it is never changed by anything done inside methods/objects/etc that you call.
However the thing that confuses people here is that when you pass an object you actually pass a reference to that object by value.
Which is what is giving you the behavior here.
You have passed a reference to the object into the list. The reference has been passed by value. That means it has been copied, and you can change the reference (but not the thing it points to) as much as you like and the method you called will not see any change.
Upvotes: 0
Reputation: 964
After obj1 = obj2;
, first instance of object E is destroyed because both of obj1 and obj2 are pointing to the same instance (the one from line 2 E obj2 = new E();
).
Upvotes: 0
Reputation: 62864
No, when adding an object to a List
, the reference is copied and therefore when you assign obj1
with new value, the list will contain the old one.
Upvotes: 4
Reputation: 41200
ArrayList is as same as Array excepts its dynamic expending features.
ArrayList is container, internally it is having a array inside it, which stores object reference.
myList.add(obj1);
This will store the reference value not reference to list.
obj1 = obj2;
This will does not effect any change in list.
Upvotes: 0