• 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

boxing/unboxing operations on equals()

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is another one...this time using equals() method.



Object class equals() method is used to test object equality ie. to see if two instances of the same type are "meaningfully equal".

Q. The above doesn't compile saying:

Why int pi in the code above not boxed (wrapped) to an Integer object while invoking equals()? The code compiles and runs when the same expression is changed to i.equals(pi), in which case the argument pi IS wrapped.

Regards
Ravinder
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can not invoke a method from a primitive type. I guss that's the reason.

Correct me if i'm wrong.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Obviously you cannot dereference a primitive. But why is it not boxed and compared with equals?
That in my opinion is a rather inefficient way of doing things.
First box an int then compare the values bitwise of two ints contained in two Integer objects?
Nonsense! The language designers are trying keep the language efficient and try to prevent
the programmer writing code that is "nonsense".

The other way around using Integer.equals() on i and boxing the argument pi from int to Integer. That is (in my opinion) an OK scenario but still inefficient.
i could have been obtained from a Collection and you want to know if it's value is pi. So you would simply have to unbox i and compare it with pi (i.intValue() == pi).
 
Ravinder Singh
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry (and Ganesh) for your clarifications...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic