Lesden, Kim wrote:

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,
No, your answer is incorrect in several places. I thought that maybe you were confused between references and objects, but the fact that you use the word "references" correctly in the first sentence suggests to me that you seriously misunderstand how the JVM treats literal Strings.

When you have

String str1 = "String";
String str2 = "String";

The references point to the SAME object. There are NOT two String objects pointing two one literal String. There are two variables (or references) of type String pointing to the same instance of a String object. Literal strings in the code are references to String objects. Further the String objects are not stored with the program code.

From the Java Lang Spec
3.10.5 String Literals
A string literal always refers to the same instance (�4.3.1) of class String.

4.3.3 The Class String
Instances of class String (�20.12) represent sequences of Unicode characters. A String object has a constant (unchanging) value. String literals (�3.10.5) are references to instances of class String.

It is true that literal String references are never garbage collected, etc. It is not true that String objects "know" this. What "knows" this is the compiler and the JVM. It is the JVM that takes all the literal strings from the byte code, constructs the String objects, and then stores these objects in a common pool. Thus, if two different classes in two different packages use the same String literal, the JVM uses the same String instance.

When a thread of execution enters the scope of str1 and str2, two String objects are not allocated. Two String variables (or references) are created. They reference the same String object. That String object has the value of the given literal string. Because no new objects are created when you declare a string variable, as you claim, then there are no objects to be garbage collected when you set the reference to null or assign a different object to the reference.

Finally, here is a simple class file:

public class StrExample {
public void myMethod() {
String str1 = "String1";
String str2 = "String1";
String str3 = new String("String1");
}
}

And here is the byte code created by the Sun JDK 1.4 compiler:

Method void myMethod()
0 ldc #2 <String "String1">
2 astore_1
3 ldc #2 <String "String1">
5 astore_2
6 new #3 <Class java.lang.String>
9 dup
10 ldc #2 <String "String1">
12 invokespecial #4 <Method java.lang.String(java.lang.String)>
15 astore_3
16 return

You can see that the compiler created code in line 6 to create a new instance of String, and line 12 is the call to the constructor for the String class. Where is the equivalent code for the declarations of str1 and str2?

Kevin Mukhar

Lesden, Kim wrote:

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
____________________________________________________




____________________________________________________
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