• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string question

 
kay lin
Ranch Hand
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sanju Thomas
Ranch Hand
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Soni Prasad
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it so because Hello is in String pool?? and is not created through new String().
[ April 14, 2005: Message edited by: Soni Prasad ]
 
Mikael Bellec
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic