| Author |
immutable String objects
|
Rahil Hurzuk
Greenhorn
Joined: Dec 28, 2004
Posts: 7
|
|
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
Joined: Apr 14, 2004
Posts: 10336
|
|
|
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.
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Jeroen Wenting
Ranch Hand
Joined: Oct 12, 2000
Posts: 5093
|
|
|
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).
|
42
|
 |
 |
|
|
subject: immutable String objects
|
|
|