aspose file tools
The moose likes Beginning Java and the fly likes immutable String objects Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "immutable String objects" Watch "immutable String objects" New topic
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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: immutable String objects
 
Similar Threads
Problem with String and StringXxx objects
Doubt in Garbage Collection
generics question (sample mocks)
Garbage collect with String Objects
is this object eligible for garbage collection??