• 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

A Question about Integer i;

 
Ranch Hand
Posts: 133
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I encountered a question,which comprises of following code snippet.



There is no compilation error.
But at the runtime an

java.lang.NullPointerException

is thrown.

If I replace Integer with int, it works fine.

I was wondering about why this happens?

Thanks,
Rohit


 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When it is written Integer i, i is an Object, and an Object gets initialized to null when no value is provided. When it is written as int i, i is a primitive int and int defaults to 0 when not assigned.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer is an Object, And as this it has the default value null. So you compare null with 0 and this will give you the NullpointerException,
If you replace it with the primitive type int, it will have the default value of 0. And so the comparison is correct.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Viktor Pergjoka wrote:Integer is an Object, And as this it has the default value null. So you compare null with 0 and this will give you the NullpointerException,



It's not the comparison that throws NPE, it's the auto-unboxing. The compiler inserts a call to i.intValue(), and since i is null, that throws NPE. Comparing null to 0 would be a compile-time error.

 
rohit chavan
Ranch Hand
Posts: 133
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Viktor Pergjoka wrote:Integer is an Object, And as this it has the default value null. So you compare null with 0 and this will give you the NullpointerException,



It's not the comparison that throws NPE, it's the auto-unboxing. The compiler inserts a call to i.intValue(), and since i is null, that throws NPE. Comparing null to 0 would be a compile-time error.



Great!
Perfect answer, thank you Jeff!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're quite welcome!
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Comparing null to 0 would be a compile-time error.


I don't think I quite follow this. I get no compiler error from the following:
... so that must not be what you meant.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Deems wrote:

Jeff Verdegan wrote:Comparing null to 0 would be a compile-time error.


I don't think I quite follow this. I get no compiler error from the following:



Because you're not comparing 0 to null. You're comparing 0 to the int result of Integer.intValue(). Change the zero variable's type to Object so there's no auto-unbox and you'll get a compiler error.

Or even simply if (null == 0)

reply
    Bookmark Topic Watch Topic
  • New Topic