| Author |
AutoBoxing Integer to int conversion
|
Praveen Palaniswamy
Greenhorn
Joined: Feb 09, 2011
Posts: 3
|
|
int a = (Integer) null;
throws a null pointer exception. Is this documented in javadoc? also if any one could point out why is it not assigned 0 which will be the default for an int.
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
Hi and welcome to the Javaranch.
What happens is that you have an Integer reference with you want to convert into an int value. This happens by calling the intValue() method on the Integer reference.
But because the Integer reference is null it results in a NullPointerException.
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
It's in the Java™ Language Specification . Look what it says about null on that page.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
To show in code what Wouter explained, your line of code is the same as this:
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: AutoBoxing Integer to int conversion
|
|
|