Search code examples
javastringvalue-typereference-type

How to justify Strings in java as reference type? check out the following cases


Case 1:

        String s1 = "Hello";
        String s2 = s1; //now has the same reference as s1 right?

        System.out.println(s1); //prints Hello
        System.out.println(s2); //prints Hello

        s1 = "hello changed"; //now changes s2 (so s1 as well because of the same reference?) to  Hello changed

        System.out.println(s1); //prints Hello changed 
        System.out.println(s2); //prints Hello  (why isn't it changed to  Hello changed?)

output of this case is obvious.

Case 2:

        String s1 = "Hello";
        String s2 = s1; //now has the same reference as s1 right?

        System.out.println(s1); //prints Hello
        System.out.println(s2); //prints Hello

        s2 = "hello changed"; //now changes s2 (so s1 as well because of the same reference?) to  Hello changed

        System.out.println(s1); //prints Hello (why isn't it changed to Hello changed?)
        System.out.println(s2); //prints Hello changed

I want to clear the confusion of reference type.


Solution

  • Ok, Let me clear all the above cases for you.

    Case 1:

    when you created String s1 = "Hello";, the compiler first places the String value "Hello" in the memory location and stores the reference of that memory location to variable s1. So your variable s1 has what? only the reference of "Hello". So s1 does not contain Hello instead it's memory location as reference. Done?

    Then when you declare String s2 = s1;, you only store the reference of Hello to s2 not the reference of s1 variable itself because s1 had the reference of "Hello" nothing else. Then when you are printing both s1 and s2 they both are printing "Hello" because they both contain the reference of "Hello".

    When you declare s1 = "hello changed";, s1 has no more the reference of "Hello" now, instead it contains the reference of String value "hello changed" which is in a different memory location. But still s2 has the reference of String value "Hello" because you did not assign anything to s2 yet. So now s1 has the reference of "hello changed" and s2 has the reference of "Hello". If you print s1 and s2, they will print their corresponding String values "hello changed" and '"Hello"`. Are you clear now? if not yet see below code samples:

    String s1 = "Hello"; // s1 has reference of "Hello"
    String s2 = s1; // Now s2 has reference of "Hello" not s1
    
    System.out.println(s1); // Prints "Hello"
    System.out.println(s2); // Prints "Hello"
    
    s1 = "hello changed"; // Now s1 has reference of "hello changed" not "Hello" but still s2 has reference of "Hello" because you did not changed it.
    
    System.out.println(s1); // Prints "hello changed" 
    System.out.println(s2); // Prints "Hello" because you did not changed it.
    

    Case 2:

    String s1 = "Hello"; // s1 has reference of "Hello"
    String s2 = s1; // Now s2 has reference of "Hello" not s1
    
    System.out.println(s1); // Prints "Hello"
    System.out.println(s2); // Prints "Hello"
    
    s2 = "hello changed"; // Now s1 has reference of "hello changed" not "Hello" but still s2 has reference of "Hello" because you did not changed it.
    
    System.out.println(s1); // Prints "Hello" because you did not changed it.
    System.out.println(s2); // Prints "hello changed" because you changed it.
    

    If you are still confused, do not forget to comment it. Thank you.