File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equality test ??? Guide me

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
swarna dasa
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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 ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice explanation,

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

Share these two cups:

Thank you all!

cmbhatt
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic