What is .intValue() in Java?

l.get(i); will return Integer and then calling intValue(); on it will return the integer as type int.

Converting an int to Integer is called boxing.
Converting an Integer to int is called unboxing
And so on for conversion between other primitive types and their corresponding Wrapper classes.

Since java 5, it will automatically do the required conversions for you(autoboxing), so there is no difference in your examples if you are working with Java 5 or later. The only thing you have to look after is if an Integer is null, and you directly assign it to int then it will throw NullPointerException.

Prior to java 5, the programmer himself had to do boxing/unboxing.

Leave a Comment