File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Strings and garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings and garbage collection" Watch "Strings and garbage collection" New topic

Strings and garbage collection

Jason Cupp

Joined: Jan 28, 2011
Posts: 7
If I create a String and change it several times, like this:

The value of name ends up being Steve. But since Strings are immutable, the way I understand it is that I've actually created four Strings and I've only changed the reference that the name instance points to.

So, if I have a more complicated program that may modify strings thousands of times, is it actually leaving all of those separate strings in memory, or does the garbage collector clean them up when nothing references them?
Markas Korotkovas

Joined: Jan 27, 2011
Posts: 21

Very good discussion on the same topic here Link
Jason Cupp

Joined: Jan 28, 2011
Posts: 7
Thank you. I'll post my questions in that thread. Sorry I didn't notice it. The title wasn't really descriptive.
I agree. Here's the link:
subject: Strings and garbage collection
jQuery in Action, 3rd edition