• Post Reply Bookmark Topic Watch Topic
  • New Topic

immutable String objects  RSS feed

 
Rahil Hurzuk
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All


A newbie question

Say if I have a String objects

String s = "nothing special"; //line1
s = s.toUpperCase(); //line2

Now the s object reference in line2 is a new one & different from the 1 created initially at line1 as Strings are immutable objects

My question is what happens to the previous String object (s) in this case

Do respond

Rgrds

Rahil
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It still exists in memory, though now there is no reference pointing to it so you can't use it, and presumably it will get Garbage Collected at some time.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings in the String object pool don't typically get garbage collected unless the pool runs into capacity problems (in which case the JVM can decide to do something, possibly based on last access time).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!