Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the need of an intValue() method if wrappers use unboxing?

For example, look at this code:

Integer myInt = new Integer(5);
int i1 = myInt.intValue();
int i2 = myInt;
    
System.out.println(i1);
System.out.println(i2);

As you can see, I have two ways of copying my integer value from the wrapper to the primive:

I can use unboxing,

OR

I can use the method Integer#intValue().

So what's the need of having a method when there is already unboxing?

like image 639
user1883212 Avatar asked Apr 13 '13 08:04

user1883212


1 Answers

Unboxing was introduced in Java 5. The wrappers (including this method) have been there since the original release.

A link to the Javadoc

In that time (1996) we did need the intValue() method and as Oracle guarantees backward backwards compatibility... up to a certain level (it is not always 100% on major releases).

The method has to stay in.

like image 68
Frank Avatar answered Nov 08 '22 10:11

Frank