• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

toString() Method

 
Harry D'Souza
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
preeti khane
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
harry,
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
suresh guru
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
public class ToStr {
public static void main(String Args[]) {
Byte b = new Byte("127") ;
if ((b.toString()).intern() == (b.toString()).intern() )
System.out.println("True");
else
System.out.println("False");
}
}

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
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :-)
-Harry.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic