Setting equal in Java: by value or reference?

前端 未结 4 1758
长发绾君心
长发绾君心 2020-12-17 00:29

I did two tests, the first starting with Strings

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

    System.out.println(         


        
4条回答
  •  天命终不由人
    2020-12-17 00:39

    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 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 list1 = new ArrayList();
    List list2 = list1;
    list1 = new ArrayList();
    list1.add(1);
    
    System.out.println(list1.size()); //1
    System.out.println(list2.size()); //0
    

    because list1 = new ArrayList(); 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).

提交回复
热议问题