Java 中的基元类型

发布于 2024-09-09 06:57:40 字数 81 浏览 2 评论 0原文

为什么在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 技术交流群。

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

发布评论

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

评论(4

醉南桥 2024-09-16 06:57:40

你的问题问反了。原始类型比它们的包装器更基本。

实际上,包装器为您提供的唯一有用的东西是能够被视为 Object 的子类(以便它们可以放入集合等中)。所有真正有用的东西(如算术和排序)都是由原始类型提供的。

请注意,虽然您可以这样说:

Integer i = Integer.valueOf(4);
Integer j = Integer.valueOf(2);
Integer k = i + j;

这只是为了方便。在下面,最后一行变成类似:

Integer k = Integer.valueOf(i.intValue() + j.intValue());

这样算术就发生在原始值上。 (这种便利称为装箱/拆箱。)这样做会带来性能损失,因此在我的机器上,此循环:

for (int i=0; i<10000000; i++) { }

比此循环快大约 10 倍:

for (Integer i=0; i<10000000; i++) { }

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:

Integer i = Integer.valueOf(4);
Integer j = Integer.valueOf(2);
Integer k = i + j;

this is just a convenience. Underneath, the last line becomes something like:

Integer k = Integer.valueOf(i.intValue() + j.intValue());

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:

for (int i=0; i<10000000; i++) { }

is about 10 times faster than this loop:

for (Integer i=0; i<10000000; i++) { }
榆西 2024-09-16 06:57:40

在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 (an int) 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, its length member, and then about 41 bytes for every Byte instance (40 bytes of overhead, plus 1 byte for data). Altogether, that's over 41k! Much more than a primitive byte[].

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.

左耳近心 2024-09-16 06:57:40

性能,原始类映射到处理器,而无需经历那么多操作。
对于 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)

-小熊_ 2024-09-16 06:57:40

[幽默]为了省打字!这个:

    int i = 4;
    int j = 38;
    int k = i + j;

比:

    Integer i = new Integer(4);
    Integer j = new Integer(38);
    Integer k = i + j;

[/幽默]

更短,实际上,其他答案说得比我更好。

[humor] To save typing! This:

    int i = 4;
    int j = 38;
    int k = i + j;

is shorter to type than:

    Integer i = new Integer(4);
    Integer j = new Integer(38);
    Integer k = i + j;

[/humor]

In reality, the other answers say it better than I could.

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