This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toString() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "toString()" Watch "toString()" New topic
Author

toString()

Ram Murthy
Ranch Hand

Joined: Aug 02, 2005
Posts: 91
1: Byte b1 = new Byte("127");
2:
3: if(b1.toString() == b1.toString())
4: System.out.println("True");
5: else
6: System.out.println("False");

A) Compilation error, toString() is not avialable for Byte.
B) Prints "True".
C) Prints "False".

The o/p is C.

Why <<b1.toString() == b1.toString()>> not true ??

Thanks,
JP


Cheers,
Ram
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Each call to the toString() method returns a new distinct String object. Two distinct objects are not "==" to each other.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ram Murthy
Ranch Hand

Joined: Aug 02, 2005
Posts: 91
I tried the below code ...

Byte b1 = new Byte("127");
System.out.println(b1.toString());
System.out.println(b1.toString());

Both the SOP's returned 127, I don't see what is distinct here ??

Regards,
JP
Kevin Lam
Ranch Hand

Joined: Oct 27, 2005
Posts: 68
But when you compare with ==, it compares the "reference address"

Although two Strings contains the same letters, but it is two different objects, hence two different addresses.

If you compare them with equals(), that will return you true because that compares the content of the two strings instead of the address.

Hope this helps

Kev
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Ravi,
Each call to the toString() method returns a new distinct String object. Two distinct objects are not "==" to each other.

What Barry meant above is that the "==" checks for reference equality and not for equality of what the Byte objects hold in this case the value 127.
Let's look at the following code diagramatically,

Byte b1 = new Byte("127");
A object is created on the heap with reference A0


System.out.println(b1.toString());
The code above will create a new object on the heap, with reference A2


System.out.println(b1.toString());
The code above will create a new object on the heap, with reference A3


As you can see the references A2 and A3 are not the same, therefore '==' returns false.
Ram Murthy
Ranch Hand

Joined: Aug 02, 2005
Posts: 91
Thanks I got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toString()
 
Similar Threads
Q.9 from Majji's paper 1
Why this prints false?
String Question
Kumar #9
related to "=="