说类型是“装箱的”是什么意思?

发布于 2024-08-04 04:46:12 字数 95 浏览 5 评论 0原文

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

在Java中,我听说过“自动装箱”。这是什么?它有类型的包装类吗?如果我使用装箱或未装箱类型,我的代码会发生什么变化?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

冬天的雪花 2024-08-11 04:46:12

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

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

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

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

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.

一张白纸 2024-08-11 04:46:12

有关 Java 的更多具体信息:

自动装箱允许 java 自动将 booleanint 等内容转换为其对象版本 BooleanInteger 在大多数情况下。它还允许相反的情况发生。

例如:

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

没有自动装箱功能的较旧版本的 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

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

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

现在,如果您尝试执行此操作:

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

这不起作用的原因是因为它无法确定是否应该将 a 拆箱为 an int 或将 b 框为 Integer。因此,应该调用哪个方法签名是不明确的。要解决此问题,您可以执行以下操作之一:

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

More specific information for Java:

Autoboxing allows java to automatically convert things like boolean and int to their Object versions Boolean and Integer in most cases. 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:

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:

assertEquals((int) a, b);
assertEquals(a, (Integer) b);
孤凫 2024-08-11 04:46:12

是的,装箱意味着采用值类型并将其包装在引用类型中。由于java引入了自动装箱,你可以这样做:

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

并且java会自动将int 1转换为Integer。在以前的版本中,您必须这样做:

foo(new Integer(1));

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

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

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:

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.

心不设防 2024-08-11 04:46:12

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

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.

你怎么敢 2024-08-11 04:46:12

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

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.

零時差 2024-08-11 04:46:12

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

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

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.

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.

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