In your first code, yes, this line
String str2 = str1;
Assigns str2
to the same String
referred by str1
, that is, "old"
. At this point, they are the same object. However, the next line
str1 = "new";
create a new instance of String
, and changes the reference of str1
to this new String. As we are changing the reference of str1
, the content of str2
are not changed.
Pay attention that Java, String
s are immutable i.e. cannot change state once initialized. Thinking this way, content of "old"
may never change. So when you assign "new"
to str1
, you don't change the value of "old"
, you create a new String
instead.
In other words, this line, in here, is the same as
str1 = new String("new");
http://i.minus.com/jboQoqCxApSELU.png
However, in the second code,
List<Integer> list2 = list1;
make list2
refer to the same list as list1
. As a result, list1
and list2
refer to the same list. Then
list1.add(1);
adds an element to the list referred by list1
. However, as I have said, list1
and list2
refer to same list, both list1
and list2
now have the element 1
. There is no new instance created in the method call.
http://i.minus.com/jxDLyBqcUzgHZ.png
In fact, if you were to do
List<Integer> list1 = new ArrayList<Integer>();
List<Integer> list2 = list1;
list1 = new ArrayList<Integer>();
list1.add(1);
System.out.println(list1.size()); //1
System.out.println(list2.size()); //0
because list1 = new ArrayList<Integer>();
reassigns list1
to a new list, that no longer refer to the object referred by list2
.
After all the assignment operator (i.e. obj1 = obj2
) always copy the references, which two references will still refer to the same object instance after the assignment. This is for both String
, List
, or any other classes (But not primitive types).
However, str1 = "new"
will, in most cases, create a new instance of String
and then assign the reference to the new String
to str1
- this is a special case in the Java lanaguage. This don't apply to any other kind of objects. This is different to any other method call like list1.add(1)
.