Will this change reflect in ArrayList?
Yes, since you added a link to the object in the list. The added link will still point to the same object (which you changed).
or when I add an object to ArrayList, does Java create a copy and add it to ArrayList?
No, it will not copy the object. (He will copy the link to the object.)
What if I change the reference to this object to null? Does this mean that the object in the ArrayList is now also null?
No, because the contents of the original link were copied when added to the list. (Keep in mind that this is a copy, not an object.)
Demonstration:
StringBuffer sb = new StringBuffer("foo"); List<StringBuffer> list = new ArrayList<StringBuffer>(); list.add(sb); System.out.println(list);
aioobe Aug 16 2018-11-18T00: 00Z
source share