在 64 位机器上,Java 32 位或 64 位中 int 的大小是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/400477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
On a 64-bit machine is the size of an int in Java 32 bits or 64 bits?
提问by jon077
On a 64-bit machine is the size of an int in Java 32 bits or 64 bits?
在 64 位机器上,Java 32 位或 64 位中 int 的大小是多少?
采纳答案by Thomas Jones-Low
32 bits. It's one of the Java language features that the size of the integer does not vary with the underlying computer. See the relevant section of the spec.
32 位。整数的大小不随底层计算机而变化是 Java 语言的特性之一。请参阅规范的相关部分。
回答by Phil
32 bits. Java is meant to be run the same regardless of the machine or OS it is run on, and at certainly this is true for primitive data types, at the very least.
32 位。无论运行在何种机器或操作系统上,Java 都应该以相同的方式运行,至少对于原始数据类型来说当然是这样。
回答by Chad Okere
Yup, it's 32 bits
是的,它是 32 位的
回答by Powerlord
If you want a 64-bit integer, use a long.
如果需要 64 位整数,请使用 long。
回答by mfx
That's one of the consequences of the "compile once, run anywhere" slogan: Java execution is independent of underlying hardware word-size and endian-ness; the JVM works everywhere the same way.
这是“一次编译,随处运行”口号的后果之一:Java 执行独立于底层硬件字长和字节序;JVM 以相同的方式在任何地方工作。
This independence guarantee works a lot better than the attempt to abstract the OS away ;-)
这种独立性保证比将操作系统抽象出来的尝试要好得多;-)
回答by erickson
The size of primitive data is part of the virtual machine specification,and doesn't change. What will change is the size of object references, from 32 bits to 64. So, the same program will require more memory on a 64 bit JVM. The impact this has depends on your application, but can be significant.
原始数据的大小是虚拟机规范的一部分,不会改变。将改变的是对象引用的大小,从 32 位到 64 位。因此,相同的程序在 64 位 JVM 上将需要更多内存。这产生的影响取决于您的应用程序,但可能很重要。
回答by svpino
Another point that is usually misunderstood is the fact that the size of these data types is going to be always the same regardless of the architecture our program is running on. So you might be running your program in a 64 bit machine, but an int is going to be always 32 bit long so it will have the same range. This is true for both C# and Java.
另一个通常被误解的事实是,无论我们的程序在何种架构上运行,这些数据类型的大小都将始终相同。因此,您可能在 64 位机器上运行您的程序,但 int 将始终为 32 位长,因此它具有相同的范围。这对于 C# 和 Java 都是如此。
Here is a full article on the matter:
这里有一篇关于这个问题的完整文章: