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

时间:2022-01-27 16:59:34

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中,我听说过“autoboxing”。这是什么?它是否有类型的包装类?如果我使用盒装或非盒装类型,我的代码会如何变化?

6 个解决方案

#1


51  

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 ......至少在我这样做的时候回来......)不允许任何原始类型,并认为一切都是一个对象,但是由于最终导致性能损失,因为最后当天,处理器需要使用原始数字和原始内存来完成有用的工作。如果要添加两个已装箱的整数,例如,在幕后将它们“取消装箱”为基本类型,则会添加数字,然后将它们装箱回新的整数。

#2


14  

More specific information for Java:

Java的更多具体信息:

Autoboxing allows java to automatically convert things like boolean and int to their Object versions Boolean and Integer automatically in most cases. It also allows the reverse to happen.

在大多数情况下,Autoboxing允许java自动将boolean和int之类的东西自动转换为Object对象版本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 a to an int or box b to an Integer. Therefore it is ambiguous which method signature should be called. To fix this you can do one of these:

这不起作用的原因是因为它无法弄清楚它是应该将int打包到int还是将b打开到整数。因此,应该调用哪个方法签名是不明确的。要解决此问题,您可以执行以下操作之一:

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

#3


12  

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.

在使用泛型时,autoboxing在java中最有用,因为你不能使用泛型的原语,所以要在列表中存储int,你必须创建一个List 并将int放入列表框中。

#4


3  

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.

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

#5


0  

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这样的语言中,基元和对象之间存在差异。当一个原语被“装箱”时,它本质上只是一个原语的包装器,因此它与期望一个Object的框架的其余部分一起使用。

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.

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

#6


0  

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.

盒装表示它们采用常规值类型并在其周围创建了一个对象。有点像把它放在一个盒子里。由于构造对象的开销,通常要避免这种情况。

#1


51  

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 ......至少在我这样做的时候回来......)不允许任何原始类型,并认为一切都是一个对象,但是由于最终导致性能损失,因为最后当天,处理器需要使用原始数字和原始内存来完成有用的工作。如果要添加两个已装箱的整数,例如,在幕后将它们“取消装箱”为基本类型,则会添加数字,然后将它们装箱回新的整数。

#2


14  

More specific information for Java:

Java的更多具体信息:

Autoboxing allows java to automatically convert things like boolean and int to their Object versions Boolean and Integer automatically in most cases. It also allows the reverse to happen.

在大多数情况下,Autoboxing允许java自动将boolean和int之类的东西自动转换为Object对象版本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 a to an int or box b to an Integer. Therefore it is ambiguous which method signature should be called. To fix this you can do one of these:

这不起作用的原因是因为它无法弄清楚它是应该将int打包到int还是将b打开到整数。因此,应该调用哪个方法签名是不明确的。要解决此问题,您可以执行以下操作之一:

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

#3


12  

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.

在使用泛型时,autoboxing在java中最有用,因为你不能使用泛型的原语,所以要在列表中存储int,你必须创建一个List 并将int放入列表框中。

#4


3  

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.

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

#5


0  

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这样的语言中,基元和对象之间存在差异。当一个原语被“装箱”时,它本质上只是一个原语的包装器,因此它与期望一个Object的框架的其余部分一起使用。

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.

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

#6


0  

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.

盒装表示它们采用常规值类型并在其周围创建了一个对象。有点像把它放在一个盒子里。由于构造对象的开销,通常要避免这种情况。