• 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

autoboxing!

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help me to know why autoboxing do not take place here
suppose i say
int a =0;
if(a!= null)

why does it throw compiler error and not do autoboxing
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because a is a primitive and cannot be compared to null. For the same reason, if you try to autobox / unbox when a reference holds a null, you will get a NullPointerException
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think autoboxing is only done when an Integer is also present...i mean when there is a requirement of autoboxing.....i just wrote it...dont know if this makes sense
 
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
AFAIK, boxing is not done in equality and relational operators. Even if you compare an int and Integer, the Integer is unboxed, the int is not boxed...
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
checck =>> boxing-unboxing
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jacob Sonia wrote:int a =0;
if(a!= null)

why does it throw compiler error and not do autoboxing


Because it would not make much sense. What would you expect; that the int a is boxed into an Integer object, and that that Integer object would be compared with null? The result would always be false.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper Young wrote:

Jacob Sonia wrote:int a =0;
if(a!= null)

why does it throw compiler error and not do autoboxing


Because it would not make much sense. What would you expect; that the int a is boxed into an Integer object, and that that Integer object would be compared with null? The result would always be false.



Jacob,
I think for boxing operation, the compiler will intValue(), so when you write below code



compiler behind the scense use intValue() method



becoz of that, it generates run-time exception at second line (NullPointerException)

and for un-boxing, compiler will use valueOf() method, therefore the following code :



will become



Hope this helps
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic