Java 说一个类型是“盒装”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1418296/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:53:09  来源:igfitidea点击:

What does it mean to say a type is "boxed"?

javatypesterminologytype-systemsautoboxing

提问by Nick Heiner

I have heard of types being referred to as "boxed" in some languages.

我听说在某些语言中类型被称为“盒装”。

In Java, I have heard of "autoboxing". What is this? Is it having wrapper classes for a type? How would my code change if I'm working with boxed or unboxed types?

在 Java 中,我听说过“自动装箱”。这是什么?它是否具有类型的包装类?如果我使用装箱或未装箱类型,我的代码将如何更改?

采纳答案by Eric J.

Some data types are considered "primitive", meaning they are not treated like an object and don't have the properties of an object.

某些数据类型被认为是“原始的”,这意味着它们不被视为对象并且不具有对象的属性。

On most platforms, integers and characters are examples of types that are primitive but can be boxed.

在大多数平台上,整数和字符是原始类型但可以装箱的示例。

Boxing means wrapping them in an object so they have the behavior of an object.

装箱意味着将它们包装在一个对象中,以便它们具有对象的行为。

The exact meaning and behavior depends on the language you're using. Some languages (such as Smalltalk... at least waaay back when I was doing it...) don't allow any primitive types and consider everything to be an object, but there's a performance penalty associated with that because, at the end of the day, the processor needs to work with raw numbers and raw memory to do useful work. If you want to add two integers that have been boxed, for example, behind the scenes they are "unboxed" into primitive types, the numbers are added, and they are then boxed back into a new integer.

确切的含义和行为取决于您使用的语言。某些语言(例如 Smalltalk ......至少在我做的时候回过头来......)不允许任何原始类型并将所有东西都视为对象,但是存在与此相关的性能损失,因为最后目前,处理器需要处理原始数字和原始内存才能完成有用的工作。例如,如果您想将两个已装箱的整数相加,在幕后将它们“拆箱”为原始类型,然后将数字相加,然后将它们装箱回新的整数。

回答by Pascal Cuoq

A boxed type means that the values are allocated in a block on the heap and referenced through a pointer. This is good for uniformity in the implementation of the runtime (it makes it easier to have generic functions, etc), at the cost of an additional indirection.

装箱类型意味着值在堆上的块中分配并通过指针引用。这有利于运行时实现的一致性(它使得拥有泛型函数等变得更容易),但代价是额外的间接性。

回答by MattC

Generally when you work with collections, you're dealing with arrays of Objects. In languages like Java, there is a difference between a primitive and an Object. When a primitive is "boxed", it's essentially just a wrapper around a primitive so it plays nice with the rest of the framework that's expecting an Object.

通常,当您使用集合时,您正在处理对象数组。在像 Java 这样的语言中,原语和对象之间是有区别的。当一个原语被“装箱”时,它本质上只是一个原语的包装器,所以它可以很好地与需要对象的框架的其余部分一起使用。

Autoboxing is just the act of putting a primitive into an object or pulling a primitive out of an object transparently so you don't have to worry about the extra step of doing it yourself.

自动装箱只是将基元放入对象或透明地从对象中拉出基元的行为,因此您不必担心自己做的额外步骤。

回答by sepp2k

Yes, boxing means taking a value type and wrapping it in a reference type. Since java introduced autoboxing you can do:

是的,装箱意味着采用值类型并将其包装在引用类型中。由于java引入了自动装箱,您可以执行以下操作:

void foo(Object bar) {}
//...
    foo(1);

And java will automatically turn the int 1 into an Integer. In previous versions you'd have to do:

而java会自动把int 1变成Integer。在以前的版本中,您必须执行以下操作:

foo(new Integer(1));

Autoboxing is most useful in java when working with generics, since you can't use primitives with generics, so to store ints in a list, you'd have to make a List<Integer>and put the ints into the list boxed.

使用泛型时,自动装箱在 Java 中最有用,因为您不能将基元与泛型一起使用,因此要将整数存储在列表中,您必须创建 aList<Integer>并将整数放入装箱的列表中。

回答by TM.

More specific information for Java:

Java的更多具体信息:

Autoboxing allows java to automatically convert things like booleanand intto their Object versions Booleanand Integerautomatically in most cases. It also allows the reverse to happen.

自动装箱允许 java 自动将诸如boolean和 之类的东西转换int为它们的对象版本,Boolean并且Integer在大多数情况下是自动的。它还允许反向发生。

For example:

例如:

int a = 3; // no boxing is happening
Integer b = 3;  // newer versions of java automatically convert the int 3 to Integer 3
int c = b;  // these same versions also automatically convert Integer 3 to int 3

Older versions of java that do not have autoboxing will require this code to do the same thing:

没有自动装箱功能的旧版 java 将需要此代码来执行相同的操作:

int a = 3;  // works the same
Integer b = new Integer(3);  //must set up a Integer object manually
int c = b.intValue(); //must change Integer object to a primitive

However, there are some scenarios where you still have to do things manually. For example, imagine you have a class with two methods like so:

但是,在某些情况下,您仍然需要手动执行操作。例如,假设您有一个具有两个方法的类,如下所示:

assertEquals(int a, int b);
assertEquals(Object a, Object b)

Now, if you try to do this:

现在,如果您尝试这样做:

Integer a = 3;
int b = 3;
assertEquals(a, b);  // this will not compile

The reason this doesn't work is because it cannot figure out whether it should unbox ato an intor box bto an Integer. Therefore it is ambiguous which method signature should be called. To fix this you can do one of these:

这不起作用的原因是因为它无法确定是否应该取消装箱a到 anint或装箱bInteger. 因此,应该调用哪个方法签名是不明确的。要解决此问题,您可以执行以下操作之一:

assertEquals((int) a, b);
assertEquals(a, (Integer) b);

回答by Alex Baranosky

Boxed means that they took a regular value type and created an object around it. Sort of like putting it in a box. This is generally to be avoided, because of the overhead of constructing the object.

装箱意味着他们采用了常规值类型并围绕它创建了一个对象。有点像把它放在一个盒子里。由于构造对象的开销,这通常是要避免的。