• 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

What are the rules to compare Wrapper objects and primitives ?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am preparing to take the SCJP 6.0.

I am primarily using Sierra and Bates for my preparation.

I found some interesting (read those that I could not answer:) questions on boxing, and would welcome any help to understand this topic a little better.

1. Long and Integer extend Number.
***Why does 12 get boxed to an Integer object, when I am only assigning it to a Supertype Number? I mean, why did it not get Boxed to Number directly?***

Number x = 12;

May be the reason I cannot cast an Integer x to Long is because they are not on the same class tree..i.e, Long does not extend Integer, and Integer does not extend Long. Please clarify if my assumption is right.
Number y = (Long) x;

2. I can do this
int x = 1;

Number y = x;

but when I do this..
if (y == x) I get an error..
would it be safe for me to assume that, a Number object and a Integer object cannot be compared, even if Integer is ONLY a subclass of Number. I ask this question becuase the compiler does not complain when I try to do this.
int x = 1;
Integer y = x;
if (y == x)..no errors




 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vijay kaaman wrote:1. Long and Integer extend Number.
***Why does 12 get boxed to an Integer object, when I am only assigning it to a Supertype Number? I mean, why did it not get Boxed to Number directly?***


Number is an abstract class.

May be the reason I cannot cast an Integer x to Long is because they are not on the same class tree..i.e, Long does not extend Integer, and Integer does not extend Long. Please clarify if my assumption is right.
Number y = (Long) x;


Yes right

2. I can do this
int x = 1;

Number y = x;

but when I do this..
if (y == x) I get an error..
would it be safe for me to assume that, a Number object and a Integer object cannot be compared, even if Integer is ONLY a subclass of Number. I ask this question becuase the compiler does not complain when I try to do this.
int x = 1;
Integer y = x;
if (y == x)..no errors


Again Number is an abstract class so there is no autoboxin-unboxing with Number class. Integer however is a concrete sub-class of Number and autoboxing-unboxing works with Integer class...
 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vijay kaaman wrote:
2. I can do this
int x = 1;

Number y = x;

but when I do this..
if (y == x) I get an error..
would it be safe for me to assume that, a Number object and a Integer object cannot be compared, even if Integer is ONLY a subclass of Number. I ask this question becuase the compiler does not complain when I try to do this.



First of all, youre not comparing an Integer object with a Number object. You're comparing an int primitive with a Number object.
Secondly, you're using identity test, this is a little tricky when what you really want to check for is equality, since youre actually comparing the identity of the objects and not their value.

vijay kaaman wrote:
int x = 1;
Integer y = x;
if (y == x)..no errors



What happens here, is that you are comparing an Integer object with an int, which leads to the Integer being unboxed before the comparison is made, which is why you do not get an error. In your previous example, your Number object was in fact an Integer object that had been converted to a Number due to your assignment. However in that case, the JVM does not know it is an Integer since you stored it in a Number reference, so it cannot be unboxed in order to be compared.

I'd recommend reading up on wrappers in general, and also on the equality and identity tests (.equals() and '==').

// Andreas
reply
    Bookmark Topic Watch Topic
  • New Topic