88

If I have an ArrayList, and I added an object to it, and later I modified this object, will this change reflect in the ArrayList? or when I add the object to the ArrayList, Java creates a copy and add it to the ArrayList?

What if I change the reference to this object to null? Does that mean that the object in the ArrayList now null too?

aioobe
  • 413,195
  • 112
  • 811
  • 826
K''
  • 5,020
  • 7
  • 33
  • 43

4 Answers4

94

will this change reflect in the ArrayList?

Yes, since you added a reference to the object in the list. The reference you added will still point to the same object, (which you modified).


or when I add the object to the ArrayList, Java creates a copy and add it to the ArrayList?

No, it won't copy the object. (It will copy the reference to the object.)


What if I change the reference to this object to null? Does that mean that the object in the ArrayList now null too?

No, since the content of the original reference was copied when added to the list. (Keep in mind that it is the reference that is copied, not the object.)

Demonstration:

StringBuffer sb = new StringBuffer("foo");

List<StringBuffer> list = new ArrayList<StringBuffer>();
list.add(sb);

System.out.println(list);   // prints [foo]
sb.append("bar");

System.out.println(list);   // prints [foobar]

sb = null;

System.out.println(list);   // still prints [foobar]
Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • what about this: Button a = new Button; Button b = new Button; Button current = a; List.add(current); current = b; If i print the list content it is gonna be "a" or "b"? – Lorenzo Sciuto Sep 10 '13 at 07:23
  • 1
    @aioobe Your answer seems to conflict with the first point in your post where you state that the reference to the object is copied (in this case, 'current'), which is then changed from a to b. I would've expected that it prints b. Can you elaborate? – Jan K. Oct 19 '13 at 15:28
  • 2
    Just keep in mind that Java is always [pass by value](http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) (in particular references to objects are passed by value) and it should all be clear. – aioobe Oct 19 '13 at 17:12
  • this doesn't seem to be the case for adding a `String` and then changing it. i.e. `String a = "first"; list.add(a); a = "second"; ...print(list.get(0)) // "first"` – Don Cheadle Mar 06 '15 at 17:09
  • I think you may want to clarify what is meant by `pass by value` as that would seem to imply that you what was sent cannot be altered as the value has been sent, not a reference to something – Don Cheadle Mar 06 '15 at 17:28
  • @mmcrae, You sound confused. *[...] the case for adding a `String` and then changing it [...]* You can't change Strings. They are immutable. – aioobe Mar 06 '15 at 18:33
7

Any change to the object will be reflected in the list.

However, when you deal with objects like Strings that are immutable, a new object will be created on "change operations". Than actually your old object is still in the list while you got a new one elsewhere.

DerMike
  • 15,594
  • 13
  • 50
  • 63
3

Thank you all guys. I figured it out again by reading your posts. It is probably a confusing concept, since I've had it digested long ago but recently I forgot how it works again. So I wanna share the tip that solves the problem for me. Note that non-primitive objects (primitives are int, boolean, etc) in java are technically pointers. When you add the 'Object o' to a list, the list's item and the 'o' point to the same thing. So when you modify 'o' (you're changing something that both 'o' & list's item are pointing to) the list's item changes too. This is as long as they point to the same thing. So this rule does not apply when 'o' points to something else after using '=' on it.

o = null;   //'o' points to nothing and from now on, changes in 'o' doesn't effect the list's item

or

Object a = new Object();
o = a;    //'o' and the list's item don't point to same thing so changes in 'o' doesn't effect the list's item (but it effects 'a')

hope it helps someone

navid
  • 1,022
  • 9
  • 20
-1

Wanted to add another demonstration where the ArrayList is inside of a Map as the value. The ArrayList is modified after adding to the Map and the Map reflects the changes.

The Map has one element with mother's name as the key and children as the value.

    String key = "adeleMom";
    Map<String, ArrayList<String>> myMap = new HashMap<String, ArrayList<String>>();
    ArrayList<String> firstList = new ArrayList<String>();
    firstList.add("adele");
    myMap.put(key, firstList);
    firstList = null;
    ArrayList secondList = myMap.get(key);
    System.out.println(secondList); // prints [adele]
    secondList.add("bonnie");
    System.out.println("Added bonnie");
    ArrayList thirdList = myMap.get(key);
    System.out.println(thirdList); // prints [adele, bonnie]
user2176745
  • 491
  • 4
  • 11