为什么 Integer 和 int 可以互换使用?

发布于 2024-11-30 13:26:19 字数 191 浏览 2 评论 0原文

我很困惑为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

春花秋月 2024-12-07 13:26:20

“可互换地”使用 intInteger 称为 自动装箱。此功能是在 Java 5 中引入的。在此之前,您的示例代码无法编译。相反,您必须编写如下内容:

Integer b = Integer.valueOf(42); // or new Integer(42);
int a  = b.intValue();

或者

int d = 12;
Integer c = Integer.valueOf(d); // or new Integer(d);

这相当冗长,这就是引入自动装箱的原因。这是编译器的一点魔力,可以让编码人员的生活变得更轻松。

从技术上讲,intInteger 本身不可互换,并且其中一个不能在需要另一个的地方使用。但是,自动装箱允许两者之间的隐式转换。

附带说明一下,有一种情况会导致自动装箱(特别是拆箱)失败。如果您的代码尝试自动取消装箱空值,您将在运行时收到 NullPointerException ,例如:

Integer b = null;
int a = b; // NullPointerException here!

需要注意的事情...

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...

夜唯美灬不弃 2024-12-07 13:26:20

它称为AutoBoxing。这将准确地解释它是什么。

It's called AutoBoxing. That will explain exactly what it is.

你好,陌生人 2024-12-07 13:26:20

除了其他答案之外,因为 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.

心是晴朗的。 2024-12-07 13:26:20

java语言规范规定java虚拟机必须执行整数和其他一些类型的自动装箱/拆箱

The java language specification states that the java virtual machine must perform automatic boxing/unboxing for integers and a few other types.

鱼忆七猫命九 2024-12-07 13:26:19

所发表文章的前几句话描述得很好:

您不能将 int (或其他原始值)放入集合中。集合只能保存对象引用,因此您必须将原始值装入适当的包装类(对于 int 来说是 Integer)。当你从集合中取出对象时,你会得到你放入的Integer;如果需要 int,则必须使用 intValue 方法取消装箱 Integer。所有这些装箱和拆箱都很痛苦,而且会让你的代码变得混乱。自动装箱和拆箱功能使整个过程自动化,消除了痛苦和混乱。

简而言之,基本上就是这样。它允许您利用基元的集合框架,而无需自己完成这项工作。

主要缺点是它会让新程序员感到困惑,如果不正确理解和使用,可能会导致混乱/混乱的代码。

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.

小…楫夜泊 2024-12-07 13:26:19

Java 支持 自动装箱 并自动将原始值包装到对象中并解开对象某些类型的原始值,例如 char - Characterint - Integerdouble- 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文