• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

equals() method for Byte class

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The explanation for public boolean equals(Object obj)method of Byte class in Java API Website is:......
Returns: true if the objects are the same; false otherwise.
but
Byte b1=new Byte((byte)1);
Byte b2=new Byte((byte)1);
boolean b=b1.equals(b2);
System.out.println(b);
output: true.
So is the explanation on Java API Website misleading? The same for the Short class.
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It returns true if the objects are the same, not the values of the objects. Since you are creating two new Byte objects, equals returns false.
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Angela,
but it returns true.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
isn't the implementation of .equals() up to the authors discretion? equals should be used for a practical comparison of the class in question. Think of the how the string class implements equals - it tests the VALUE, not the reference. I think the only restriction is that an equals() should test that the comparison is against an instance of the same class that the equals() method belongs to.
 
Angela Lamb
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I guess I didn't read carefully enough. The API does seem to be misleading in this case.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sort of depends on what you think that "the same" means.
This is the actual code that is implemented in the Byte method


* Compares this object to the specified object.
*
* @param obj the object to compare with
* @return true if the objects are the same; false otherwise.
*/
public boolean equals(Object obj) {
if ((obj != null) && (obj instanceof Byte)) { return value == ((Byte)obj).byteValue();
}
return false;
}



It is looking to see if the VALUES are the same.

[This message has been edited by Cindy Glass (edited June 04, 2001).]
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To me, "the same" as regards Objects means the same Object (i.e., the reference is to the same address in memory). When the API means the same value, it generally is explicit about that -- e.g., check out how the API describes the Boolean equals() method. In the case of Byte/Short, I think the API documentation is misleading.
Bin, I deleted my original post as soon as I posted because I realized I had referenced the wrong class. I guess you checked the thread in the 5 seconds it was actually there.
[This message has been edited by Scott Appleton (edited June 04, 2001).]
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Cindy and Scott.
The explanation for equals() method of Float class and Double class on the Java API Website is pretty clear. I just don't know why people who create the java API documents didn't explain the equals() method of Byte and Short class the same way. Maybe there are some reasons for that.
Anyway, What I have learned is: for all the six wrapper classes, the equals() method is comparing the values.

 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic