This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes string question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "string question" Watch "string question" New topic
Author

string question

kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
Hi
JSK 1.3 for the java.lang.String class states:
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared


I am just reviewing and preparing for the exam now. I am still a bit confused with the subject of String.

Suppose you have the following

String str = "Hello";
str = "dog";

then, I would assume that a reference to the string "dog" is assigned to str, but a reference to string "hello" still lives on the heap.

Is my interpretation correct? please let me know

Many thanks
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
Now the string object having the value of hello will be eligible for garbage collection, Because the reference varible str is now refering to the string object having the value of dog. So the first string object will be remain in the heap until the garbage collector destroy it.
MI Mohammed
Ranch Hand

Joined: Feb 16, 2005
Posts: 146
Hello,

String in java are objects.
When a string is created, the heap is checked
to see if that String object exit on the heap.
If it does, the ref points to it. And if it does't
a new String object is created.eg

String str = "abcd"; // place abcd on the heap --> str
str = "hit"; // str --> hit, and abcd for gc
str = str + "def"; // new String is created "hitdef"
//and hit for gc
Hope it helped.
cheers

SCJP 1.4


SCJA(Beta) SCJP 1.4 SCWCD 1.4 SCBCD 1.3 SCBCD 5.0 beta <br />The more practice we get, the better we are at the exams and in life in general. Pls join me at My DEN.
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243

String in java are objects.
When a string is created, the heap is checked
to see if that String object exit on the heap.
If it does, the ref points to it. And if it does't
a new String object is created.eg



In this case you are right,

String str ="hello";
String str1 = "hello"

But in this case, a new String object will be created.

String str = "hello";
String str1 = new String("hello");

Am I right ?
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

Now the string object having the value of hello will be eligible for garbage collection

No, it won't.
It will only be eligible for garbage collection if the class loader that loaded the class becomes eligible for garbage collection.


Tony Morris
Java Q&A (FAQ, Trivia)
Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
Is it so because Hello is in String pool?? and is not created through new String().
[ April 14, 2005: Message edited by: Soni Prasad ]

SCJP 1.4, SCBCD 1.3
Mikael Bellec
Greenhorn

Joined: Nov 27, 2001
Posts: 6
Correct.

String created with new String("hello") will follow normal GC rules.
All string using primitive references (str = "hello" will be part of the pool and unloaded with the class loader I believe.

Rgds,
M
 
wood burning stoves
 
subject: string question
 
Similar Threads
use of quotes in type-in qts
Strings immutable
Passing a String reference to a method.
Another Question in GC
Strings question