java 自动装箱:所以我可以写:Integer i = 0; 而不是:Integer i = new Integer(0);
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/766468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Autoboxing: So I can write: Integer i = 0; instead of: Integer i = new Integer(0);
提问by Harry Quince
Autoboxing seems to come down to the fact that I can write:
自动装箱似乎归结为我可以写的事实:
Integer i = 0;
instead of:
代替:
Integer i = new Integer(0);
So, the compiler can automatically convert a primitive to an Object.
因此,编译器可以自动将原语转换为对象。
Is that the idea? Why is this important?
是这个想法吗?为什么这很重要?
回答by Brian Gianforcaro
You kind of simplified it a little too much.
你有点过于简化了。
Autoboxing also comes into play when using collections. As explained in sun's java docs:
使用集合时,自动装箱也会发挥作用。正如 sun 的 java 文档中所解释的:
Collections can only hold object references, so you have to box primitive values into the appropriate wrapper class. ...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.
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.
集合只能保存对象引用,因此您必须将原始值装箱到适当的包装类中。...当你从集合中取出对象时,你会得到你放入的整数;如果需要 int,则必须使用 intValue 方法对 Integer 进行拆箱。所有这些装箱和拆箱都是一种痛苦,并且会使您的代码变得混乱。自动装箱和拆箱功能使过程自动化,消除了痛苦和混乱。
那么什么时候应该使用自动装箱和拆箱呢?仅当引用类型和基元之间存在“阻抗不匹配”时才使用它们,例如,当您必须将数值放入集合中时。对于科学计算或其他对性能敏感的数字代码,不适合使用自动装箱和拆箱。Integer 不能替代 int;自动装箱和拆箱模糊了原始类型和引用类型之间的区别,但它们并没有消除它。
回答by Peter Lawrey
BTW
顺便提一句
Integer i = 0;
is equivalent to
相当于
Integer i = Integer.valueOf(0);
The distinction is that valueOf() does not create a new object for values between -128 and 127 (Apparently this will be tunable if Java 6u14)
区别在于 valueOf() 不会为 -128 和 127 之间的值创建新对象(显然,如果 Java 6u14,这将是可调的)
回答by starblue
It exists so that you can write code like
它存在以便您可以编写代码,例如
List<Integer> is = new ArrayList<Integer>();
is.add(1); // auto-boxing
is.add(2);
is.add(3);
int sum = 0;
for (int i : is) // auto-unboxing
{
sum += i;
}
For single integers you should by default use the type int, not Integer. Integer is mostly for use in collections.
对于单个整数,默认情况下应该使用 int 类型,而不是 Integer。整数主要用于集合。
Beware that a Long is different from the same value as an Integer (using equals()), but as a long it is equal to an int (using ==).
请注意,Long 不同于与 Integer 相同的值(使用 equals()),但作为 long 它等于 int(使用 ==)。
回答by jcrossley3
That is the idea, yes. It's even more convenient to be able to assign an Integer to an int, though.
这就是想法,是的。不过,能够将 Integer 分配给 int 更加方便。
One could argue that autoboxing addresses a symptom rather than a cause. The real source of confusion is that Java's type system is inconsistent: the need for both primitives and object references is artificial and awkward. Autoboxing mitigates that somewhat.
有人可能会争辩说,自动装箱是针对一种症状而不是原因。真正令人困惑的根源在于 Java 的类型系统是不一致的:对原语和对象引用的需求是人为的和笨拙的。自动装箱在某种程度上缓解了这种情况。
回答by Humphrey Bogart
With my cynical hat on: to make-up for limitations on the original Java (I mean Oak here) spec. Not just the first time.
带着我愤世嫉俗的帽子:弥补原始 Java(我在这里指的是 Oak)规范的限制。不仅仅是第一次。
回答by CookieOfFortune
Makes for more readable and neater code. Especially if you're doing operations (Since Java doesn't have operator overloading).
使代码更具可读性和更整洁。特别是在您进行操作时(因为 Java 没有运算符重载)。
回答by Julson Lim
From what I remember from reading Joshua Bloch's Effective Java, you should consider the primitives over their boxed counterparts. Autoboxing without regard for its side effects can produce problems.
根据我阅读 Joshua Bloch 的 Effective Java 的记忆,您应该考虑原始类型而不是盒装版本。不考虑其副作用的自动装箱可能会产生问题。
回答by Humphrey Bogart
Adding to Lim's comment, primitives are stored on the stack, and primitive-wrappers, as objects, are stored on the heap... There are subtle implications due to this.
添加到 Lim 的评论中,基元存储在堆栈中,而基元包装器作为对象存储在堆中......因此有一些微妙的含义。
回答by KahWee Teng
The main advantage would be readability, syntactic sugar basically. Java is already very verbose, Sun is trying all sorts of ways to make the syntax shorter.
主要优点是可读性,基本上是语法糖。Java 已经很冗长了,Sun 正在尝试各种方法来缩短语法。

