Why can Integer and int be used interchangably?
I am confused as to why Integer and int can be 开发者_如何转开发used interchangeably in Java even though one is a primitive type and the other is an object?
For example:
Integer b = 42;
int a = b;
Or
int d = 12;
Integer c = d;
The first few sentences of the posted article describe it pretty well:
You can’t put an int (or other primitive value) into a collection. Collections can only hold object references, so you have to box primitive values into the appropriate wrapper class (which is Integer in the case of int). When you take the object out of the collection, you get the Integer that you put in; if you need an int, you must unbox the Integer using the intValue method. All of this boxing and unboxing is a pain, and clutters up your code. The autoboxing and unboxing feature automates the process, eliminating the pain and the clutter.
That is basically it in a nutshell. It allows you take advantage of the Collections Framework for primatives without having to do the work yourself.
The primary disadvantage is that it confuses new programmers, and can lead to messy/confusing code if it's not understood and used correctly.
Java supports autoboxing and automatically wraps primitive values into objects and unwraps objects to primitive values for certain types, like char
- Character
, int
- Integer
, double
- Double
, etc.
Note from Oracle Documentation:
So when should you use autoboxing and unboxing? Use them only when there is an “impedance mismatch” between reference types and primitives, for example, when you have to put numerical values into a collection. It is not appropriate to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code. An Integer is not a substitute for an int; autoboxing and unboxing blur the distinction between primitive types and reference types, but they do not eliminate it.
Because of autoboxing and autounboxing http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html
Using an int
and an Integer
"interchangeably" is called autoboxing. This feature was introduced in Java 5. Before that, your example code wouldn't have compiled. Instead, you would have to write something like this:
Integer b = Integer.valueOf(42); // or new Integer(42);
int a = b.intValue();
or
int d = 12;
Integer c = Integer.valueOf(d); // or new Integer(d);
That's fairly verbose, which is why autoboxing was introduced. It's a bit of compiler magic to make life easier for the coder.
Technically, int
and Integer
themselves are not interchangeable and one cannot be used where the other is required. However, autoboxing allows implicit conversion between the two.
As a side note, there is one case where autoboxing (specifically unboxing) fails. If your code tries to autounbox a null value, you will get a NullPointerException
at runtime, e.g.:
Integer b = null;
int a = b; // NullPointerException here!
Just something to be aware of...
It's called AutoBoxing. That will explain exactly what it is.
In addition to other answers, because Integer is a wrapper class, that lets you box and unbox an int
value. Other info here.
The java language specification states that the java virtual machine must perform automatic boxing/unboxing for integers and a few other types.
精彩评论