Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

string - Setting equal in Java: by value or reference?

I did two tests, the first starting with Strings

    String str1 = "old";
    String str2 = str1;
    str1 = "new";

    System.out.println(str1); //new
    System.out.println(str2); //old

The above example indicates that str2 = str1, by value

Now I do the similar operations, but this time with Lists

    List<Integer> list1 = new ArrayList<Integer>();
    List<Integer> list2 = list1;
    list1.add(1);

    System.out.println(list1.size()); //1
    System.out.println(list2.size()); //1

This example indicates that list2 = list1, by reference

I am confused, which Java variables/objects are passed by value and which are passed by reference?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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, Strings 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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...