aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes When String gets Equal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "When String gets Equal" Watch "When String gets Equal" New topic
Author

When String gets Equal

Nayyer Kamran
Greenhorn

Joined: Dec 15, 2000
Posts: 16
hi every one,
I am facing problem while understanding the string equlity. the following chunk of code returns
output:
false
true
Source:
public class StringEqual{
public static void main(String args[]){
String a= new String("Hello");
String b ="Hello";
System.out.println(a.toString()=="Hello");
System.out.println(b.toString()=="Hello");
}
}
Why so I want to know the logic behind what a.toString() returns to compare that result in "false" and what b.toString() returns to compare that result in "true".
thx in advance
Nayyer
vadiraj vd
Ranch Hand

Joined: Dec 15, 2000
Posts: 68
Originally posted by Nayyer Kamran:
hi every one,
I am facing problem while understanding the string equlity. the following chunk of code returns
output:
false
true
Source:
public class StringEqual{
public static void main(String args[]){
String a= new String("Hello");
String b ="Hello";
System.out.println(a.toString()=="Hello");
System.out.println(b.toString()=="Hello");
}
}
Why so I want to know the logic behind what a.toString() returns to compare that result in "false" and what b.toString() returns to compare that result in "true".
thx in advance
Nayyer

Nayyar,
In java Strings are objects and they are mantained in a pool.
Whenever we create a string object like
<PRE>
String s = "Hello";
</PRE>
the obj. ref. s is pointing to the object which is in the pool(if it is created already). But if we something like
<PRE>
String s = new String("Hello");
</PRE>
Then s refers to an object which is NOT the same object which is there in the pool.
Now come to your case.
you did
String a = new String("Hello");
and called
a.toString() which returns a new String Object.
again, Java creates one more string object when you do "Hello".
But this object refers to the pool.
When you create the second object like
String b = "Hello";
java sees that the object "Hello" is already there in the pool so it assignes the that reference to b.
So the first comparison results in false since a is pointing to object different than the object returned by a.toString() (which is the object in pool).
The second time the comparison results to true since the object b refers to the same object in the pool which is returned by b.toString().
I think this will clear your doubt.
-vadiraj


Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

the main point is that
String b ="Hello";
creates a String object in the string pool.
but
String a= new String("Hello");
creates the new String object on the heap along with the other objects.
also note that:
b=="Hello" is true
a=="Hello" is false
b.equals("Hello") is true
a.equals("Hello") is true


SCJP
Visit my download page
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When String gets Equal