File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == operator 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" Watch "== operator" New topic
Author

== operator

Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 353
Generel this is a comparison on the object reference, thats why i don�t understand this code !
Byte b1 = new Byte("127");
if(b1.toString() == b1.toString())
System.out.println("True");
else
System.out.println("False");
Here is the result false, but why, here returns the b1.toString methoed "127" and they are both in the String pool and pointing on the same String Object or ???
Can anyone tell me why this code prints "True";
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Simply because the toString method returns a new reference to the string, and although, the referenced string are the same, the references themselves are different. Comparing with == only compares the "value" of the references themselves and not the values they are referencing.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 353
Thanks !!!
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Isn't it because each toString() method returns a new String object? I don't understand the term 'new reference'.
Owee Nicolas
Ranch Hand

Joined: Jun 16, 2002
Posts: 49
You can make your code to return "True" if you did something like:
Byte b1 = new Byte("127");
String s1, s2;
s1 = b1.toString();
s2 = s1;
if(s1 == s2)
System.out.println("True");
else
System.out.println("False");
wherein you assign s2 to the same reference as s1.


Owee<br />SCJP 1.4
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Isn't it because each toString() method returns a new String object? I don't understand the term 'new reference'.
In fact, the case of String is particular. toString() does in fact return a new reference, BUT the referenced string should already be in the constant string pool somewhere. If you will, "127" is put into the string pool. Then b1.toString() returns one reference to "127" and the second b1.toString() invocation returns another reference to "127". Finally, when == is used, the value of the references themselves are compared and not the value they are referencing (i.e. "127"). References are more or less addresses in memory which contain the address of the value they are referencing, much like pointers. == compares the address of the references and not the address they are pointing to.
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Woah, I never heard of that before. Will it make a difference if I stick with my explanation, i.e. will believing that a new String object is made by the toString() method instead of the 'new reference' thingy make some wrong answers right (and vice versa)? My exam's tomorrow, and last-minute cramming isn't gonna help me any.
manasa teja
Ranch Hand

Joined: May 27, 2002
Posts: 325
My exam's tomorrow, and last-minute cramming isn't gonna help me any.[/QB]

Paul,
All the best for your exam. plz do not forget to share your experience with ranchers
thanks
murthy


MT
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
will believing that a new String object is made by the toString() method instead of the 'new reference' thingy
Believe what helps you the most
I wish you good luck for tomorrow
Come back with good news...
 
 
subject: == operator