In your sample you have two String references str1 and str2 pointing to two _different_ String objects pointing to _one_ literal "String". The literal "String" is normally stored together with the program code and _not_ on the heap, since it's content and size is known at compile time and will never change. As such it never needs to be garbage collected, nor is there any need for reference counting. The String objects "know" this (through the constructor used by the program to create them).
 
When the program enters the scope of str1 and str2, two normal String objects are allocated (the code for this is generated automatically by the compiler). If you would set them to null oder a different String object (str1 = null or str1 = "hello") then, assuming you haven't set any other references to the object (str3 = str1), the "old" object would be marked for garbage collection.
 
There's no magic behind it.
 
Regards
Kim
-----Urspr�ngliche Nachricht-----
Von: abhay [mailto:[EMAIL PROTECTED]]
Gesendet: Donnerstag, 6. Februar 2003 08:36
An: JDJList
Betreff: [jdjlist] RE: String and other classes

"Burnett, David" wrote:
 String str1 = "String";
 String str2 = "String";The str1 and str2 handles will often point to the same object (depending on how the compiler deals with String literals),
I have a question here to ask.. If the 2 objects actually point to the same memory location
then how does the interpreter keep the track of the references attached to it ?
Doe it use any kind of 'reference counting mechanism' ?

How will that object be garbage collected ?

Please throw light on this issue. (Even some link to some site will help.)

Thankyou in advance.

regards
abhay ____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm

Be respectful! Clean up your posts before replying
____________________________________________________

____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm

Be respectful! Clean up your posts before replying
____________________________________________________

Reply via email to