• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

OCA Java SE 8 Programmer - Question on String immutability

 
Ranch Hand
Posts: 127
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic