为什么 Integer 和 int 可以互换使用?
我很困惑为什么 Integer 和 int 在 Java 中可以互换使用,即使一个是原始类型,另一个是对象?
例如:
Integer b = 42;
int a = b;
或者
int d = 12;
Integer c = d;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
由于自动装箱和自动拆箱 http://download.oracle.com/javase/1.5 .0/docs/guide/language/autoboxing.html
Because of autoboxing and autounboxing http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html
“可互换地”使用
int
和Integer
称为 自动装箱。此功能是在 Java 5 中引入的。在此之前,您的示例代码无法编译。相反,您必须编写如下内容:或者
这相当冗长,这就是引入自动装箱的原因。这是编译器的一点魔力,可以让编码人员的生活变得更轻松。
从技术上讲,
int
和Integer
本身不可互换,并且其中一个不能在需要另一个的地方使用。但是,自动装箱允许两者之间的隐式转换。附带说明一下,有一种情况会导致自动装箱(特别是拆箱)失败。如果您的代码尝试自动取消装箱空值,您将在运行时收到 NullPointerException ,例如:
需要注意的事情...
Using an
int
and anInteger
"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:or
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
andInteger
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.:Just something to be aware of...
它称为AutoBoxing。这将准确地解释它是什么。
It's called AutoBoxing. That will explain exactly what it is.
除了其他答案之外,因为 Integer 是一个包装类,可让您装箱和取消装箱
int
值。其他信息此处。In addition to other answers, because Integer is a wrapper class, that lets you box and unbox an
int
value. Other info here.java语言规范规定java虚拟机必须执行整数和其他一些类型的自动装箱/拆箱。
The java language specification states that the java virtual machine must perform automatic boxing/unboxing for integers and a few other types.
所发表文章的前几句话描述得很好:
简而言之,基本上就是这样。它允许您利用基元的集合框架,而无需自己完成这项工作。
主要缺点是它会让新程序员感到困惑,如果不正确理解和使用,可能会导致混乱/混乱的代码。
The first few sentences of the posted article describe it pretty well:
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 支持 自动装箱 并自动将原始值包装到对象中并解开对象某些类型的原始值,例如
char
-Character
、int
-Integer
、double-
Double
等。来自 的注释Oracle 文档:
那么什么时候应该使用自动装箱和拆箱呢?仅当引用类型和基元之间存在“阻抗不匹配”时才使用它们,例如,当您必须将数值放入集合中时。对于科学计算或其他性能敏感的数字代码,不适合使用自动装箱和拆箱。 Integer 不能替代 int;自动装箱和拆箱模糊了原始类型和引用类型之间的区别,但并没有消除它。
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.