简体   繁体   English

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

[英]What does it mean to say a type is “boxed”?

I have heard of types being referred to as "boxed" in some languages.我听说在某些语言中类型被称为“盒装”。

In Java, I have heard of "autoboxing".在 Java 中,我听说过“自动装箱”。 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?如果我使用装箱或未装箱类型,我的代码将如何更改?

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

More specific information for Java: Java的更多具体信息:

Autoboxing allows java to automatically convert things like boolean and int to their Object versions Boolean and Integer in most cases.在大多数情况下,自动装箱允许 java 自动将诸如booleanint类的内容转换为它们的对象版本BooleanInteger It also allows the reverse to happen.它还允许反向发生。

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 .这不起作用的原因是因为它无法确定是否应该将a拆箱为int或将b拆箱a Integer Therefore it is ambiguous which method signature should be called.因此,应该调用哪个方法签名是不明确的。 To fix this you can do one of these:要解决此问题,您可以执行以下操作之一:

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

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.而java会自动把int 1变成Integer。 In previous versions you'd have to do:在以前的版本中,您必须执行以下操作:

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 中最有用,因为您不能将基元与泛型一起使用,因此要将整数存储在列表中,您必须创建一个List<Integer>并将整数放入装箱的列表中。

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.这有利于运行时实现的一致性(它使得拥有泛型函数等变得更容易),但代价是额外的间接性。

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.在像 Java 这样的语言中,原语和对象之间是有区别的。 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.当一个原语被“装箱”时,它本质上只是一个原语的包装器,所以它可以很好地与需要对象的框架的其余部分一起使用。

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

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.由于构造对象的开销,这通常是要避免的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM