File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes question about strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question about strings" Watch "question about strings" New topic
Author

question about strings

Jay Dilla
Ranch Hand

Joined: Aug 12, 2004
Posts: 201
had this question in a phone interview
i think i answered it right but not sure

anyway the question was if you have a string variable called name
and you put a value in it
then you put another value into it, what happens to the first value

so something like:



what happens to John?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

In the general case, it's simply no longer referenced by the variable, and may be eligible for garbage collection if nothing else references it. In the specific case you've shown, though, it's a String literal, which the JVM keeps in a special "String pool", so that it won't be collected. If the String was created in some other way, though, it might be.


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
This is an interesting area. Strings are objects like any others. But strings that are also "constants" get special treatment. See Strings, Literally here on the ranch for details.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jay Dilla
Ranch Hand

Joined: Aug 12, 2004
Posts: 201
O I said it's on the heap somewhere floating around and ready for System.gc() to grab it up. What is this thing about "String pool"? How would I have to write it in order for it to be eligible for garbage collection?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The String pool is a special collection of Strings which are defined in class files; the JVM uses it so such things can be shared and not duplicated.

A String literal -- a double-quoted String -- will always be stored in the String pool, and won't be collected (at least, in theory, until the class that defines it is collected.) There's no way to remove it from the pool.

Any other String -- one that comes from new String(), from BufferedReader.readLine(), etc, etc, -- will behave as you've described.

EXCEPT if you call intern() on it -- which will copy it into the String pool, and return that pooled copy.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about strings
 
Similar Threads
Counter Instance Variable
HashCode and Equals
Getting the Hashtable values by partial key
print a parameter name and value (with a function)
meaningfully equivalent variables