• Post Reply Bookmark Topic Watch Topic
  • New Topic

How does an immutable String get changed?  RSS feed

 
femi Joseph
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read that String is immutable but why does it change in this code.

String a = "abc"; //String a is immutable so it doest change which makes a remain "abc"
String b = a.toUpperCase();// b is immutable and is set to be "ABC" . it doesn't change

b=b.replace("B","2").replace('C','3');

system.out .println("a="+a); //this prints out abc if I am correct

System.out.println("b="+b);//this prints out A23.


since b is immutable and is also set to be ABC. why does it change

 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

b is now a reference to the immutable string "ABC". That string cannot be altered, but b can be changed to refer to a different String:

Here, b.replace("B","2") creates a NEW immutable String by replacing "B" with "2" from the original string. then .replace("C","3") creates another new immutable String with the specified replacement from the first new String.
And finally the "b=..." reassigns variable b to refer to that second new String.
The original String "ABC" (as well as the first new interim String "A2C") now have no references to them, so are now available for garbage collection.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:. . . The original String "ABC" ...[is]... now available for garbage collection.
Are you sure about that? Don't String literals remain in the heap for the entire life of that JVM?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to understand the difference between variables and objects. They are not the same thing. Saying things like "a is a String object", or "b is a String object", is sloppy language and incorrect. a and b are variables that refer to objects; they are not objects themselves.

When you do b = b.replace(...) then you are making the variable b refer to a different String object. The content of the original String object does not change - it cannot, because class String is immutable, which means that the content of a String object cannot change after it has been created.

So, no String objects are being changed here. You are just making the variable refer to a different String object.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't String literals remain in the heap for the entire life of that JVM?
I hadn't particularly thought of that, but  believe you are right. Probably the generated interim Stings are the only ones that will be garbage collected.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!