• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OCA Java SE 8 Programmer - Question on String immutability

 
Raghavendra Desoju
Ranch Hand
Posts: 95
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I read "Once a String object is created, it is not allowed to change. It cannot be made larger or smaller, and you cannot change one of the characters inside it".

Below code produces the output and it doesn't throw any compile time errors:



Output:

1
12
1aaa

If String is immutable compiler should not compile the above code right? Does it mean that the Object in memory gets garbage collected and new Object gets created every time when we re-assign a different value?

Thanks, Raghu
 
Paweł Baczyński
Bartender
Posts: 1876
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String object is immutable. String variable can be changed (unless it is declared as final) to refer to other String object.
If you have and thenthat does not modify the original string "a". It creates new string "ab" and assigns it to variable a.

You can see that String is immutable in your own example.
On line 5 you executed s2.concat("3") (s2 is "12" at the time)
On line 7 you printed s2 and the output was 12. The String object was not modified.
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
method inside String that looks like modify String object just create new String object. Same thing when you use +

also pay note when you assign result to some variable and when not. (when not you loose result of creation new String)
also you may interesting to this: JLS: 15.28. Constant Expressions - this will help when you understanding why:

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavendra Desoju wrote:If String is immutable compiler should not compile the above code right? Does it mean that the Object in memory gets garbage collected and new Object gets created every time when we re-assign a different value?

Luckily it doesn't! Otherwise many, many applications would not compile successfully anymore

When you make any change to a String (add a few characters, replace a character, remove a character, make it upper/lower case,...), a new String object will be created. If you don't assign this new String object to a reference variable, this new String object will be lost and be eligible for garbage collection. Here's a little exampleThis code prints "Java rocks!". Although on line1 you upper cased the String object (referred by s), s isn't changed at all, it still refers to the original String. So String is really immutable!

Now let's change the previous code snippet a little and see what happens?On line1 the result of the toUpperCase method is assigned to reference variable s and now the expected "JAVA ROCKS!" is printed.

And to make sure there's no misunderstanding. In both code snippets 2 String objects are created: "Java rocks!" and "JAVA ROCKS!", so it's not just 1 String object which is changed. No! 2 String objects are created! So although String is immutable, you can still change String objects but each change results ALWAYS in a new String object and the original String object does NOT change at all.

And as a final note: I can recommend reading this excellent article about the immutability of String, String literals and the String Literal Pool.

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic