Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
655 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...