• 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

K & B q3 chapter 6. Wrappers

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

I have problems understanding why this returns true. I have taken this from the K & B book's self test.

The output is : true

IMHO it's comparing two object references: a Short with an Integer, which I would think you can't do. The only thing I can imagine is that it unboxes wrapper objects for comparison. Actually, that's what K & B says for the integer, but it says that s1 is already a primitive...
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

My take on this is that auto unboxing happens when you apply a comparison operator on 2 wrapper instances.
Then implicit widening rule applies and promotes the short value 7 to an int. Java then resolves 7 < 8 to true.
 
Alex Serna
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what I think too. Thank you for your response!
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two different wrapper objects can be used in comparison operators but not with equality operators. like == or !=
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
shiva ,
is it a rule or what ?
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my explaination but i am not sure it may be wrong,if so please correct me.

if you are using two different wraper object with the equality operator,it instead trying to unbox them java will try to look if they are pointing to the same object but here the object types are different(not from the same inheritance tree) and hence compiler throws error.

But if you are using two different wrapper Objects in the comparison operator it uses .xxxValue to convert to the largest type used depending upon the Wrapper.

E.g. if both the wrapper type is less than Integer then their intValue method is called,if any one of them is Long then longValue is called etc.

So finally the comparison is happening only on primitive and hence no error.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simple example, create a cat and dog class.....now create references of these classes and then try to use the equality operator. Well here we cannot use the other operators too as there is no feature called unboxing or autoboxing but Wrappers are regular classes only.
 
Alex Serna
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Siva Masilamani wrote:Two different wrapper objects can be used in comparison operators but not with equality operators. like == or !=



Thank you. I didn't know that I'll write it down in red so that I don't forget.
 
Alex Serna
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Siva Masilamani wrote:Two different wrapper objects can be used in comparison operators but not with equality operators. like == or !=



but if I have a "Wrapper (== or !=) primitive", like

will work.

So, to make a general rule... If you have a comparison of two Wrapper objects it will work by unboxing and comparing except if you have == or !=. If you have a comparison of a Wrapper and a primitive it will work anyway because the Wrapper will unbox and it will be like comparing two primitives.

Please correct me if I'm wrong. I think this would be an important rule for the exam.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you got it.

If any of the operand is primitive then the other one will be converted into primitive and comparison will be done on primitive only.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is possible to use == with wrappers. If;
Integer i=3;
Integer j=3;

i==j gives true

But
Integer i=new Integer(3);
Integer j=new Integer(3);

i==j gives false

So it is possible to use ==, as long as the wrapper uses the pool instead of making a new object.
 
Alex Serna
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Linda Wiklund wrote:It is possible to use == with wrappers. If;
Integer i=3;
Integer j=3;
...
So it is possible to use ==, as long as the wrapper uses the pool instead of making a new object.


That's true until you reach 128, thats when you stop using the pool.
 
Are we home yet? Wait, did we forget the tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic