Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toString() Method 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 "toString() Method" Watch "toString() Method" New topic

toString() Method

Harry D'Souza

Joined: Dec 28, 2002
Posts: 28
There is this question that I found on one of the Practise tests and am sort of confused.
The Question is: what gets printed when the below code is executed ?

When this code is executed, the string False is printed to the screen.
Could somebody please explain as to why it is False that gets printed ? My first thought was that the String True would get printed.
[ April 21, 2003: Message edited by: Harry D'Souza ]

Harry D'Souza<br />SJCP 1.4 , SCWCD
preeti khane
Ranch Hand

Joined: Mar 12, 2003
Posts: 93
the toString() method creates a new object reference of the Byte class. Hence the '==' operator will compile and at runtime give False... It's the same concept for all wrapper classes
However if you were comparing 2 objects of different types(Byte and Integer) the compiler would give an error... The '==' operator returns true if the objects operands are of the same type and also point to the same object reference
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
Hi Harry
At first it might look as if true is the answer. But remember that would return a String type of obect. That is two new String type of objects are created and hence it should be checked using equals() and not ==.
As explained in the following code

I guess i am right if not please corret me
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
This is what I think is happening - but I'm not 100% sure.
When b.toString() is first invoked, a String object is created on the heap and there is also a hidden intern to the literal pool, meaning that there atre now two String objects. However, the reference is to the object on the heap.
When b.toString() is invoked again, the compiler does not create a second, identical String object on the heap or in the pool. This would be pointless as Strings are immutable and can therefore be shared. So, a reference is instead returned to the String object in the pool.
The == comparison returns false because this is always what happens for pool/heap comparisons.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
suresh guru
Ranch Hand

Joined: Sep 07, 2002
Posts: 38
public class ToStr {
public static void main(String Args[]) {
Byte b = new Byte("127") ;
if ((b.toString()).intern() == (b.toString()).intern() )

the toString method returns the String reperesentation of the Byte value. Since the returned type is a reference to the Streing objs, they cant be compared using == operator but when we use intern() method, It compares the constants & resurns true
Harry D'Souza

Joined: Dec 28, 2002
Posts: 28
Hi Everybody,
Thanks a lot for explaining this. All the above explainations were great.
For whatever reason, it never struck me that toString() method was returning a new String Object. I sure will try to remember that :-)
I agree. Here's the link:
subject: toString() Method
It's not a secret anymore!