说一个类型是“盒装”是什么意思? [英] What does it mean to say a type is "boxed"?

查看:374
本文介绍了说一个类型是“盒装”是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

在Java中,我听说过autoboxing。这是什么?它是否有类型的包装类?如果我使用盒装或非盒装类型,我的代码会如何变化?

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?

推荐答案

某些数据类型被认为是原始的,意思是它们不像对象那样对待,也没有对象的属性。

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.

确切的含义和行为取决于您使用的语言。有些语言(比如Smalltalk ......至少在我这样做的时候回来......)不允许任何原始类型并认为所有东西都是一个对象,但是由于最终导致了性能损失,因为最后当天,处理器需要使用原始数字和原始内存来完成有用的工作。如果你想添加两个已装箱的整数,例如,在幕后将它们拆箱成基本类型,就会添加数字,然后将它们装回一个新的整数。

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.

这篇关于说一个类型是“盒装”是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆