wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equality test ??? Guide me 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 "equality test ??? Guide me" Watch "equality test ??? Guide me" New topic
Author

equality test ??? Guide me

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


Output:
false
true
false

I don't know, but I am not able to tell myself what is going on there.
Before I go to have a cup of caffeine, I want to know, what is happening there.

The first line is more confusing, aren't both returns the same String?

Help me!!!


cmbhatt
[ April 17, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
b1.toString()==b1.toString() // return a new String with the Byte representation (both are diff references)
b1.toString().equals(b1.toString()) // using .equals, data in the string is equal
b1.toString().equals(b1)// - comparing a String with byte, instanceof fails - false
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Looking at the source code of Byte shows that the toString() method calls the String.valueOf(int) method which calls Integer.toString(int,int) which is going to create a new String object.
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
First line two different String objects are being compared.
Second line the contents of two different String objects are being compared.
Last line a String object is being compared to a Byte object.

Or at least that's what I think is going on.

Richard
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Chandra,

Line (1) > ((b1.toString()==b1.toString())
first b1.toString() create new String on heap(this reference used) and pool.
second b1.toString() creates new String on heap only.

Since both references are different. its false.


Line (2)> (b1.toString().equals(b1.toString())

First b1.toString() say string1 --> "127"
Second b1.toString() say string2 --> "127"

its clear hear string1.equals(string2) --> true

Line (3)> (b1.toString().equals(b1))

b1.toString() is a String. String has over-ridden equals.
Which will Check whether it can convert the Object to String.

Here in our case you are sending Byte.
Which will fail instanceof test and return false.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
@Richard - Almost:


First line two references to different String objects are being compared.
Second line the (identical) contents of two different String objects are being compared.
Last line a String object is being compared to a Byte object. (So it fails the instanceof test in the equals method)

Or at least that's what I think is going on.

Richard

[ April 17, 2007: Message edited by: Barry Gaunt ]

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

Joined: Feb 28, 2007
Posts: 1707
Nice explanation,

Line 3: instanceof test, oh yeah, that is why it fails.

Share these two cups:

Thank you all!

cmbhatt
 
GeeCON Prague 2014
 
subject: equality test ??? Guide me