aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == operator returns true for Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "== operator returns true for Strings" Watch "== operator returns true for Strings" New topic
Author

== operator returns true for Strings

Ravisekhar Kovuru
Greenhorn

Joined: Sep 23, 2003
Posts: 25
HI, I have the follwoing code:
String s1="java";
String s2="java";
String s3=new String("java");
System.out.println(s1==s2);
System.out.println(s1==s3);
This prints 'true' and 'false' respectively. Any explanation for this??
Thanks
-Ravi
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
s1 and s2 references point to the same object on the String pool, but s3 is created using new operator and therefore is not the same as "java" String literal. It also occupies a different location in the memory. == compares references to the objects in the same class hierarchies, whereas equals() evaluates the equality of the objects based on how equals() method is overridden. So, applying equals() will return true for any combination of s1, s2, and s3. Hope this helps.
Suresh Thota
Ranch Hand

Joined: Sep 24, 2003
Posts: 152

String s1="java";
String s2="java";
String s3=new String("java");
System.out.println(s1==s2);
System.out.println(s1==s3);

== operator compares object references. when the string "java" is assigned to s2, s2 is being pointed to the string "java" which is already on the heap. so, obviously s1 and s2 points to the same objects. Thus, (s1==s2) returns true.
Now when s3 = new String("java") gets executed, a new string gets created on the heap as you are using the new keyword to create the string. Thus, (s1==s3) returns false.
This is my first reply in Forums .Correct me if am wrong.


SCJP 1.6
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
hi, here is how to look at it
1) s1 --> "java" <-- s2
2) s3 --> "java"
1) is a compile time literal, created at compile time.
2) is a runtime literal, created at runtime.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Lei Wu
Greenhorn

Joined: Sep 22, 2003
Posts: 6
Originally posted by K. Ravisekhar Naidu:
HI, I have the follwoing code:
String s1="java";
String s2="java";
String s3=new String("java");
System.out.println(s1==s2);
System.out.println(s1==s3);
This prints 'true' and 'false' respectively. Any explanation for this??
Thanks
-Ravi

My answer:
Every string literal is an instance of class String, and only one is created if they are the same. So s1 == s2 is true, because they are both refering to the same instance of class String.
The line:

is actually calling class String's construct String(String s), so a new instance will be created. It is like:
String s = "java";
String s3 = new String(s);
Ravisekhar Kovuru
Greenhorn

Joined: Sep 23, 2003
Posts: 25
Hi everyone ! thanks for all your nice explanations. Special thanks to Mr.Vicken Karaoghlanian for explaining 'compile time' and 'run time' literal.
Yours,
Ravi.
[ September 24, 2003: Message edited by: K. Ravisekhar Naidu ]
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
posted by Vicken:
hi, here is how to look at it
1) s1 --> "java" <-- s2
2) s3 --> "java"
1) is a compile time literal, created at compile time.
2) is a runtime literal, created at runtime.

I thought both are created at runtime. Only difference is that s1 and s2 are pointing to same string in string constant pool while s3 is pointed to a different string on the heap.
Anyone, correct me if I am wrong.
Thanks
Barkat
[ September 25, 2003: Message edited by: Barkat Mardhani ]
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Posted by Barkat:

I thought both are created at runtime

objects that are created using the keyword new are runtime objects (They are always created at runtime)
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
http://www.xdweb.net/~dibblego/javafaq/javafaq.html#q25


Tony Morris
Java Q&A (FAQ, Trivia)
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Vicken,
Here is a quote from K&B page 360:
Creating New Strings
Earlier we promised to talk more about the subtle differences between the various
methods of creating a String. Let�s look at a couple of examples of how a String might
be created, and let�s further assume that no other String objects exist in the pool:
1 � String s = "abc"; // creates one String object and one reference
// variable
In this simple case, �abc� will go in the pool and s will refer to it.
2 � String s = new String("abc"); // creates two objects, and one
// reference variable
In this case, because we used the new keyword, Java will create a new String
object in normal (nonpool) memory, and s will refer to it. In addition, the literal
�abc� will be placed in the pool.

The way I read it is that in both cases string objects are created at runtime. Difference is that in first case, it is created in a special area of memory called string constant pool. In second case (with new operator), it is created in normal (nonpool) memory.
Thanks
Barkat
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
posted by Barkat

The way I read it is that in both cases string objects are created at runtime

i didn't see the part where it says that both objects are created at runtime!!!
this topic was discussed here a couple of months ago, i can't find the link for it but as far as i remember, they came up with the following:
1) String constant pool: memory allocation for compile time literals.
2) Non-pool area: memory allocation for runtime literals.
the reason they came up with 'constant pool' ides is for memory optimization, and from my personal point of view i don't think that creating all the strings at runtime will optimize anything, in fact it will be inefficient.
at least that what i think. if you disagree please explain.
[ September 25, 2003: Message edited by: Vicken Karaoghlanian ]
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Can any guru help us with this?
I think that objects in string constant pool are created at runtime just like the string objects that are created at runtime with new opearator. Vicken's understanding is that objects in string constant pool are created at compile time. I guess the key is determine time of creation
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

The compiler puts the string constants in the class file.


constant_pool[]
The constant_pool is a table of structures (�4.4) representing various string constants, class and interface names, field names, and other constants that are referred to within the ClassFile structure and its substructures.

There is something called as CONSTANT_String_info and I think it is String constant.
I believe that when the class gets loaded the string constant from the class file are created in the String pool.
]
Loading of a class or interface that contains a String literal may create a new String object (�2.4.8) to represent that literal. This may not occur if the a String object has already been created to represent a previous occurrence of that literal, or if the String.intern method has been invoked on a String object representing the same string as the literal.


Some one correct me, if I am wrong
This is my 2000th post in Javaranch
[ September 25, 2003: Message edited by: Pradeep Bhat ]

Groovy
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
so.... when they are created, at compile time or at runtime?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

At runtime.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch Suresh.
_________________________________________________________________________
I agree with Pradeep and Barkat all object are created at runtime.
The string literals and compile constant expressions (JLS 15.28) that yield a string (*) spawn a CONSTANT_Utf8_info entry in the constant pool of the class declaring them. Such entry is holding the content of the string in a modified UTF_8 format. The compiler makes any subsequent string literal or compile constant expresion holding exactly the same string content to point to such entry. These entries are not of course objects. CONSTANT_Utf8_info holds the content for string literals, full qualifed names for classes and interfaces, simple names of members, and descriptors for members and arrays.
At runtime, the first time a CONSTANT_Utf8_info entry, that holds the content of a string literal or string constant compile expression, is used, the "interpreter" looks for an entry in the string pool with the same content as the entry. If it finds it does not create a new String object, it just places the reference to the String object in the corresponding CONSTANT_String_info entry. In this way, all the "subsequent string literals with the same content" are pointing to the same String object. If there is not a match in the search of the string pool the "interpreter" creates one, adds its reference to the pool and places the reference to the new object into the corresponding constant pool entry.

(*) compile constant expression:
final String s = "from hell";
String q = "Hello" + s;


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: == operator returns true for Strings