Java immutable strings confusion

后端 未结 8 1320
粉色の甜心
粉色の甜心 2021-01-13 03:07

If Strings are immutable in Java, then how can we write as:

String s = new String();
s = s + \"abc\";
相关标签:
8条回答
  • 2021-01-13 03:28

    The first answer is absolutely correct. You should mark it as answered.

    s = s+"abc" does not append to the s object. it creates a new string that contains the characters from the s object (of which there are none) and "abc".

    if string were mutable. it would have methods like append() and other such mutating methods that are on StringBuilder and StringBuffer.

    Effective Java by Josh Bloch has excellent discussion on immutable objects and their value.

    0 讨论(0)
  • 2021-01-13 03:29

    Strings are immutable.
    That means that an instance of String cannot change.

    You're changing the s variable to refer to a different (but still immutable) String instance.

    0 讨论(0)
  • 2021-01-13 03:31
    String s = new String();
    

    An empty String object ("") is created. And the variable s refers to that object.

    s = s + "abc";
    

    "abc" is a string literal (which is nothing but a String object, which is implicitly created and kept in a pool of strings) so that it can be reused (since strings are immutable and thus are constant). But when you do new String() is totally different because you are explicitly creating the object so does not end up in the pool. You can throw is in the pool by something called interning.

    So, s + "abc" since at this point concatenation of and empty string ("") and "abc" does not really create a new String object because the end result is "abc" which is already in the pool. So, finally the variable s will refer to the literal "abc" in the pool.

    0 讨论(0)
  • 2021-01-13 03:34

    Your string variable is NOT the string. It's a REFERENCE to an instance of String.

    See for yourself:

    String str = "Test String";
    System.out.println( System.identityHashCode(str) ); // INSTANCE ID of the string
    
    str = str + "Another value";
    System.out.println( System.identityHashCode(str) ); // Whoa, it's a different string!
    

    The instances the str variable points to are individually immutable, BUT the variable can be pointed to any instance of String you want.

    If you don't want it to be possible to reassign str to point to a different string instance, declare it final:

    final String str = "Test String";
    System.out.println( System.identityHashCode(str) ); // INSTANCE ID of the string
    
    str = str + "Another value"; // BREAKS HORRIBLY
    
    0 讨论(0)
  • 2021-01-13 03:39
       String s = new String();  
    

    Creates a new, immutable, empty string, variable "s" references it.

       s = s+"abc";              
    

    Creates a new, immutable, string; the concatenation of the empty string and "abc", variable "s" now references this new object.

    0 讨论(0)
  • 2021-01-13 03:42

    Just to clarify, when you say s = s+"abc"; That means, create a new String instance (which is composed of s and "abc") then assign that new String instance to s. So the new reference in s is different from the old.

    Remember, a variable is effectively a reference to an object at some specific memory location. The object at that location stays at that location, even if you change the variable to refer to a new object at a different location.

    0 讨论(0)
提交回复
热议问题