Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object Comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object Comparison" Watch "Object Comparison" New topic
Author

Object Comparison

Paulo Freitas
Greenhorn

Joined: Feb 26, 2003
Posts: 17
I've came across some question and I'd like to have a explanation for that.

The answer is C, but I don't know why... Can someone shed some light in that question for me, please???
TIA.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974

The above prints "true". Does that make it clearer?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Paulo Freitas
Greenhorn

Joined: Feb 26, 2003
Posts: 17
Yes, it does.
thanks Thomas.
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
I didn't understand still, why in the above code b1.toString() == b1.toString() prints false?Can anybody please exaplain?


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Veena
That's because b1.toString() method returns the value at runtime so it isn't in the pool so the String comparison fails. But b1.toString().intern() causes the String to be first searched in the pool and if found the reference is returned else a new string is added to the pool and hence true.
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Originally posted by Anupam Sinha:
Hi Veena
That's because b1.toString() method returns the value at runtime so it isn't in the pool

If the string is not in the pool ,in the above code how does System.out.println(b1.toString()) prints 127,I thought b1.toString() value is 127 ,so it should print true in the above comparison.I don't understand it fully.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Veena
Think of it this way Veena. The value of b1.toString() is not available at compile time so it's not in the pool. The value is made available only at runtime so the value is not in the pool and hence the output false. To print a value there is no requirement that a String has to be in the pool. If you want you can put the value in the pool by using the intern() method. Though in case, if you replace Byte with String in the first line in the first post then the answer would be true. This is because the toString method of the String class returns itself.
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Originally posted by Anupam Sinha:
Hi Veena
Think of it this way Veena. The value of b1.toString() is not available at compile time so it's not in the pool. The value is made available only at runtime so the value is not in the pool and hence the output false. To print a value there is no requirement that a String has to be in the pool. If you want you can put the value in the pool by using the intern() method. Though in case, if you replace Byte with String in the first line in the first post then the answer would be true. This is because the toString method of the String class returns itself.

You mean string is available in the pool during compile time for String type & is not available in the pool for byte type?
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Veena
No. What I am saying is that b1.toString() computes the equivalent string value of Byte object at runtime. In the case of the Byte class a freshly prepared string is returned but in the case of the String class the value of the toString() method is still computed at runtime but the value returned is the string itself so the two toString() methods return the same thing hence true.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Let�s find out what b1.toString() does:

b1.toString calls String.valueOf(int),
which calls Integer.toString(int,int),
which calls Integer.toString(int),
which executes new String(int, int, char[]).
A new String object is created by converting the byte value in the Byte object to an array of char and passing that array to the String constructor.
This happens both times b1.toString() is called.
[ June 15, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
And here is what String.toString() does
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The toString method of Byte creates a new String on the heap. An == comparison will return false because == comparison is always an address comparison. Two different objects on the heap are never equal.
The intern method takes the String on the heap and adds it to the String pool if it doesn't exist or returns the address of the object in the String pool if it does exist. So that is why the intern'ed version returns true on == comparison. The two Strings are actually one String in the String pool and therefore they have the same address.
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Thank you everybody.It is a tricky question though.I understood.I just wanna clarify.If b1 is Byte Object or any other object except String object,b1.toString() returns String object reference,where as if b1 is String object b1.toString() returns object itself.Am I right?
Thank you all.
Veena
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The toString always returns a reference to a String. But the String.toString() method returns a reference to the very same String.
That is why, if "s" is a String
s == s.toString() is true.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Object Comparison