• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

equality test ??? Guide me

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Honk if you love justice! And honk twice for tiny ads!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic