为什么 Java API 使用 int 而不是 short 或 byte? [英] Why does the Java API use int instead of short or byte?

查看:26
本文介绍了为什么 Java API 使用 int 而不是 short 或 byte?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Java API 使用int,而short 甚至byte 就足够了?

示例:DAY_OF_WEEK 类中的字段 日历 使用int.

如果差异太小,那为什么这些数据类型(shortint)存在?

解决方案

部分原因已经指出.例如,"...(几乎)所有对 byte、short 的操作都会将这些原语提升为 int".然而,显而易见的下一个问题是:为什么这些类型被提升为int?

所以更深入一层:答案可能仅与 Java 虚拟机指令集有关.如表中所述Java 虚拟机规范所有积分算术运算,如加法、除法等,仅适用于类型int和类型long,对于较小的类型.

(旁白:较小的类型(byteshort)基本上只用于数组.一个 new byte[1000]这样的数组需要1000个字节,像new int[1000]这样的数组需要4000个字节)

现在,当然,人们可以说 "... 下一个显而易见的问题是:为什么这些指令只提供给 int(和long)?".

上面提到的JVM Spec中提到了一个原因:

<块引用>

如果每个类型化指令都支持 Java 虚拟机的所有运行时数据类型,那么指令数量将超过一个字节所能表示的数量

此外,Java 虚拟机可以被视为对真实处理器的抽象.为较小的类型引入专用的算术逻辑单元是不值得的:它需要额外的晶体管,但它仍然只能在一个时钟周期内执行一次加法.设计 JVM 时的主导架构是 32 位,正好适合 32 位 int.(涉及 64 位 long 值的操作作为特殊情况实现).

(注意:最后一段有点过于简化,考虑了可能的矢量化等,但应该给出基本思想,不要深入研究处理器设计主题)

<小时>

一个简短的附录,侧重于问题中的示例,但在更一般的意义上:人们还可以问使用较小的类型存储字段是否有益.例如,人们可能认为可以通过将 Calendar.DAY_OF_WEEK 存储为 byte 来节省内存.但是在这里,Java 类文件格式开始发挥作用:所有 类文件中的字段至少占据一个slot",其大小为一个int(32位).(宽"字段,doublelong,占据两个位置).因此,明确地将字段声明为 shortbyte 也不会节省任何内存.

Why does the Java API use int, when short or even byte would be sufficient?

Example: The DAY_OF_WEEK field in class Calendar uses int.

If the difference is too minimal, then why do those datatypes (short, int) exist at all?

解决方案

Some of the reasons have already been pointed out. For example, the fact that "...(Almost) All operations on byte, short will promote these primitives to int". However, the obvious next question would be: WHY are these types promoted to int?

So to go one level deeper: The answer may simply be related to the Java Virtual Machine Instruction Set. As summarized in the Table in the Java Virtual Machine Specification, all integral arithmetic operations, like adding, dividing and others, are only available for the type int and the type long, and not for the smaller types.

(An aside: The smaller types (byte and short) are basically only intended for arrays. An array like new byte[1000] will take 1000 bytes, and an array like new int[1000] will take 4000 bytes)

Now, of course, one could say that "...the obvious next question would be: WHY are these instructions only offered for int (and long)?".

One reason is mentioned in the JVM Spec mentioned above:

If each typed instruction supported all of the Java Virtual Machine's run-time data types, there would be more instructions than could be represented in a byte

Additionally, the Java Virtual Machine can be considered as an abstraction of a real processor. And introducing dedicated Arithmetic Logic Unit for smaller types would not be worth the effort: It would need additional transistors, but it still could only execute one addition in one clock cycle. The dominant architecture when the JVM was designed was 32bits, just right for a 32bit int. (The operations that involve a 64bit long value are implemented as a special case).

(Note: The last paragraph is a bit oversimplified, considering possible vectorization etc., but should give the basic idea without diving too deep into processor design topics)


EDIT: A short addendum, focussing on the example from the question, but in an more general sense: One could also ask whether it would not be beneficial to store fields using the smaller types. For example, one might think that memory could be saved by storing Calendar.DAY_OF_WEEK as a byte. But here, the Java Class File Format comes into play: All the Fields in a Class File occupy at least one "slot", which has the size of one int (32 bits). (The "wide" fields, double and long, occupy two slots). So explicitly declaring a field as short or byte would not save any memory either.

这篇关于为什么 Java API 使用 int 而不是 short 或 byte?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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