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

Wrapper problem

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


how come the result is false? wrapper classes override the
equals method, isnt it ?

please explain
thanks in advance
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Long has overridden equals

This is from API docs for Long.equals method

public boolean equals(Object obj)

Compares this object to the specified object. The result is true if and only if the argument is not null and is a Long object that contains the same long value as this object.

It is false since bt is not reference of Long object
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

'b' is of type Long while 'bt' is a Byte object. equals() returns false if the object types are different.

So, because b and bt have different types, b.equals(bt) will always return false.

Yes, the wrapper classes override the equals() method, but according to javadoc of equals() in wrapper class Long: "Compares this object to the specified object. The result is true if and only if the argument is not null and is a Long object that contains the same long value as this object".

Regards,
Shawnne
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what the API docs have to say about theq equals method in the Long class.

Compares this object to the specified object. The result is true if and only if the argument is not null and is a Long object that contains the same long value as this object

In your case, you are invoking the equals method of the Long class but the parameter that is passed is a Byte object. So irrespective of the value that is wrapped, the method will return false.


Arvind
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Mr Niranjan Deshpande .

This question is the right example that fits your comment.
" no good questions posted these days "
[ December 15, 2005: Message edited by: Purujit Saha ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic