Java 中的基元类型
为什么在java中使用原始类型而不是包装类?我想知道java中已经有了包装类,那么为什么我们需要使用原始类型呢? java中原始类型的重要性是什么?
Why to use primitive types in java instead of Wrapper classes? I want to know that already we have wrapper classes in java, then why we need to use primitive types? What are the importance of primitive types in java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的问题问反了。原始类型比它们的包装器更基本。
实际上,包装器为您提供的唯一有用的东西是能够被视为 Object 的子类(以便它们可以放入集合等中)。所有真正有用的东西(如算术和排序)都是由原始类型提供的。
请注意,虽然您可以这样说:
这只是为了方便。在下面,最后一行变成类似:
这样算术就发生在原始值上。 (这种便利称为装箱/拆箱。)这样做会带来性能损失,因此在我的机器上,此循环:
比此循环快大约 10 倍:
Your question is backwards. The primitive types are more fundamental than their wrappers.
Really the only useful thing that wrappers give you is the ability to be treated as a subclass of Object (so that they can be put into collections and so on). All the really useful stuff (like arithmetic and ordering) is provided by the primitive type.
Note that while you can say stuff like:
this is just a convenience. Underneath, the last line becomes something like:
so that the arithmetic occurs on the primitive values. (This convenience is known as boxing/unboxing.) There is a performance penalty to this, so that on my machine, this loop:
is about 10 times faster than this loop:
在Java中,每个对象都有一些空间开销。它取决于 JVM 以及目标体系结构上“字”的大小(64 位与 32 位),但我通常估计每个对象都有大约 40 字节的“开销”,超出了其字段所需的大小。
因此,例如,1024 字节的数组是一个对象,有 40 字节的开销;它有一个
length
成员(一个int
),需要 4 个字节,每个元素需要 1 个字节,总共大约 1k。1024 个 Byte 实例的数组具有数组本身、其 length 成员的开销,以及每个 Byte 实例大约 41 个字节(40字节的开销,加上 1 个字节的数据)。总共超过 41k!不仅仅是原始的
byte[]
。由于包装器类型是不可变的,因此有人建议采用巧妙的技巧,使原始数据看起来像包装的对象实例,但到目前为止,这些机制都没有实现。
In Java, every object has some space overhead. It depends on the JVM, and the size of a "word" on the target architecture (64-bit versus 32-bit), but I usually estimate that every object has about 40 bytes of "overhead" beyond what is needed for its fields.
So, for example, an array of 1024 bytes is one object, with 40 bytes of overhead; it has a
length
member (anint
) which needs 4 bytes, and it needs 1 byte for each element, for a total of around 1k.An array of 1024
Byte
instances has the overhead of the array itself, itslength
member, and then about 41 bytes for everyByte
instance (40 bytes of overhead, plus 1 byte for data). Altogether, that's over 41k! Much more than a primitivebyte[]
.Since the wrapper types are immutable, there have been suggestions for clever tricks to make primitive data look like wrapped object instances, but so far, none of these mechanisms have been implemented.
性能,原始类映射到处理器,而无需经历那么多操作。
对于 boolean、int 和 float - 您可以使它们可变并确保它们不需要同步(读取和写入是原子的)
performance, primitive classes map to the processor without having to go through as many operations.
In the case of boolean, int and float - you can make them volatile and ensure that they do not require synchronization (Reads and writes are atomic)
[幽默]为了省打字!这个:
比:
[/幽默]
更短,实际上,其他答案说得比我更好。
[humor] To save typing! This:
is shorter to type than:
[/humor]
In reality, the other answers say it better than I could.