Immutability of Strings in Java

2018-12-31 01:41发布

Consider the following example.

String str = new String();

str  = "Hello";
System.out.println(str);  //Prints Hello

str = "Help!";
System.out.println(str);  //Prints Help!

Now, in Java, String objects are immutable. Then how come the object str can be assigned value "Help!". Isn't this contradicting the immutability of strings in Java? Can anybody please explain me the exact concept of immutability?

Edit:

Ok. I am now getting it, but just one follow-up question. What about the following code:

String str = "Mississippi"; 
System.out.println(str); // prints Mississippi 

str = str.replace("i", "!"); 
System.out.println(str); // prints M!ss!ss!pp! 

Does this mean that two objects are created again ("Mississippi" and "M!ss!ss!pp!") and the reference str points to a different object after replace() method?

24条回答
深知你不懂我心
2楼-- · 2018-12-31 02:01

If HELLO is your String then you can't change HELLO to HILLO. This property is called immutability property.

You can have multiple pointer String variable to point HELLO String.

But if HELLO is char Array then you can change HELLO to HILLO. Eg,

char[] charArr = 'HELLO';
char[1] = 'I'; //you can do this

Programming languages have immutable data variables so that it can be used as keys in key, value pair.

查看更多
还给你的自由
3楼-- · 2018-12-31 02:02

String is immutable. Which means that we can only change the reference.


String a = "a";
System.out.println("String a is referencing to "+a); // Output: a

a.concat("b");
System.out.println("String a is referencing to "+a); // Output: a

a = a.concat("b");
System.out.println("String a has created a new reference and is now referencing to "+a); // Output: ab
查看更多
柔情千种
4楼-- · 2018-12-31 02:04

The object that str references can change, but the actual String objects themselves cannot.

The String objects containing the string "Hello" and "Help!" cannot change their values, hence they are immutable.

The immutability of String objects does not mean that the references pointing to the object cannot change.

One way that one can prevent the str reference from changing is to declare it as final:

final String STR = "Hello";

Now, trying to assign another String to STR will cause a compile error.

查看更多
余欢
5楼-- · 2018-12-31 02:05

Regarding the replace part of your question, try this:

String str = "Mississippi"; 
System.out.println(str); //Prints Mississippi 

String other = str.replace("i", "!"); 
System.out.println(str); //still prints Mississippi 
System.out.println(other);  // prints M!ss!ss!pp!
查看更多
深知你不懂我心
6楼-- · 2018-12-31 02:08

String class is immutable, and you can not change value of immutable object. But in case of String, if you change the value of string than it will create new string in string pool and than your string reference to that value not the older one. so by this way string is immutable. Lets take your example,

String str = "Mississippi";  
System.out.println(str); // prints Mississippi 

it will create one string "Mississippi" and will add it to String pool so now str is pointing to Mississippi.

str = str.replace("i", "!");  
System.out.println(str); // prints M!ss!ss!pp! 

But after above operation, one another string will be created "M!ss!ss!pp!" and it will be add to String pool. and now str is pointing to M!ss!ss!pp!, not Mississippi.

so by this way when you will alter value of string object it will create one more object and will add it to string pool.

Lets have one more example

String s1 = "Hello"; 
String s2 = "World"; 
String s = s1 + s2;

this above three line will add three objects of string to string pool.
1) Hello
2) World
3) HelloWorld

查看更多
梦该遗忘
7楼-- · 2018-12-31 02:08

String in Java in Immutable and Final just mean it can't be changed or modified:

Case 1:

class TestClass{  
 public static void main(String args[]){  
   String str = "ABC";  
   str.concat("DEF");  
   System.out.println(str);  
 }  
} 

Output: ABC

Reason: The object reference str is not changed in fact a new object "DEF" is created which is in the pool and have no reference at all (i.e lost).

Case 2:

class TestClass{  
 public static void main(String args[]){  
   String str="ABC";  
   str=str.concat("DEF");  
   System.out.println(str);  
 }  
}  

Output: ABCDEF

Reason: In this case str is now referring to a new object "ABCDEF" hence it prints ABCDEF i.e. previous str object "ABC" is lost in pool with no reference.

查看更多
登录 后发表回答