如何在java中声明一个32位整数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4065776/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:10:51  来源:igfitidea点击:

How do I declare a 32bit integer in java?

javainteger32bit-64bit

提问by Joel Coehoorn

I'm looking at an app in java that runs on both 32bit and 64bit systems, and it deals mainly with IP addresses. These IP addresses are kept as integers, and the normal type for this wastes a ton of memory on 64bit platforms (and yes, memory use has already been shown to be an issue here). How do you declare 32bit integral values that stay at 32bits even on x64 architectures?

我正在查看一个在 32 位和 64 位系统上运行的 Java 应用程序,它主要处理 IP 地址。这些 IP 地址被保存为整数,在 64 位平台上,这种正常类型会浪费大量内存(是的,内存使用在这里已经被证明是一个问题)。您如何声明即使在 x64 架构上也保持在 32 位的 32 位整数值?

As I'm a java novice, if there's a built-in type that's already designed to handle IP addresses please feel free to point that out to me ;)

由于我是 Java 新手,如果有已经设计用于处理 IP 地址的内置类型,请随时向我指出;)

采纳答案by paxdiablo

Java has specificwidths for its data types, for portability. Integers are 32 bits wide even on 64-bit platforms. The Java language specificationstates quite clearly(slightly paraphrased to make more readable, in my opinion):

为了可移植性,Java对其数据类型有特定的宽度。即使在 64 位平台上,整数也是 32 位宽。在Java语言规范规定很清楚(略转述,使更具可读性,在我看来):

The integral types are byte, short, int, and long, whose values are respectively 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, and char, whose values are 16-bit unsigned integers representing UTF-16 code units.

整数类型有 byte、short、int 和 long,它们的值分别是 8 位、16 位、32 位和 64 位有符号补码整数,和 char,它们的值是 16 位无符号整数,表示UTF-16 编码单元。

This is arguably due to the many portability issues discovered in C with the different widths (and even encodings like two's complement, ones' complement and sign/magnitude) of integers that Java hoped to avoid, especially since it was meant to run on as many different platforms as possible.

这可以说是由于在 C 中发现了许多可移植性问题,Java 希望避免整数的不同宽度(甚至编码,如二进制补码、补码和符号/大小),特别是因为它旨在运行在尽可能多的整数上尽可能不同的平台。

As to a built-in type for handling IP addresses, Java has both Inet4Addressand Inet6Address, depending on your needs (and their common ancestor, InetAddress).

至于用于处理 IP 地址的内置类型,Java 有Inet4AddressInet6Address,这取决于您的需要(以及它们的共同祖先InetAddress)。

回答by pyCoder

int primitive type of java have a fixed size of 32bit

java的int原始类型具有32位的固定大小

回答by Stephen C

Use the primitive inttype. It is 32 bit on all Java platforms; see JLS 4.2.1.

使用原始int类型。它在所有 Java 平台上都是 32 位的;见JLS 4.2.1

and yes, memory use has already been shown to be an issue here

是的,内存使用已经被证明是这里的一个问题

Well, if you say so.

好吧,如果你这么说。

We don't know what you are doing here. But if you are concerned about the memory usage in representing huge numbers of IP addresses in memory, then maybe you need to:

我们不知道你在这里做什么。但是,如果您担心在内存中表示大量 IP 地址时的内存使用情况,那么您可能需要:

  • not keep the IP addresses in memory,
  • increase your Java heap size, or
  • (if necessary) up-size your hardware or operating system.
  • 不将 IP 地址保存在内存中,
  • 增加您的 Java 堆大小,或
  • (如有必要)升级您的硬件或操作系统。

Or maybe you've just gotten memory "wastage" out of proportion. Java is intrinsically memory hungry, and the pragmatic answer is to let it have what it wants.

或者,也许你刚刚得到了不成比例的内存“浪费”。Java 本质上是内存饥渴的,务实的答案是让它拥有它想要的东西。

FOLLOW UP

跟进

Java does not have an unsigned inttype. But you don't need that to represent IP addresses, not least because you don't need to do arithmetic on them.

Java 没有无符号int类型。但是您不需要用它来表示 IP 地址,尤其是因为您不需要对它们进行算术运算。

(And even if you did, there are ways to do it. Bear in mind that if you want to squeeze the last ounce of memory "wastage" out of a Java application, you are going to need to do some ugly things.)

(即使你这样做了,也有办法做到这一点。请记住,如果你想从 Java 应用程序中挤出最后一盎司的内存“浪费”,你将需要做一些丑陋的事情。)

回答by Gintautas Miliauskas

From http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html:

http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html

The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing Unicode characters.

整数类型有 byte、short、int 和 long,它们的值分别是 8 位、16 位、32 位和 64 位有符号补码整数,以及 char,它们的值是 16 位无符号整数表示 Unicode 字符。

So intis already 32-bit by definition, although the interpreter could be putting it on a 64-bit boundary in memory in order to speed up access.

因此,int已经通过定义32位,虽然解释可以把它在64位边界上的内存,以加快进入。

回答by vcsjones

"int" / "Integer" in Java is not platform dependent. It's always 32-bit. The platform dependent one is com.sun.jna.ptr.IntByReference.

Java 中的“int”/“Integer”与平台无关。它始终是 32 位。依赖于平台的一个是 com.sun.jna.ptr.IntByReference。

回答by Slava Markeyev

In java ints are 32 bit already. Longs are 64 bit.

在 java 中,整数已经是 32 位了。多头是 64 位。

http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

回答by Zecc

Don't worry, the inttype in Java is defined (unlike in C/C++) to be exactly 32-bit, as mentioned here.

不用担心,intJava 中的类型被定义为(与 C/C++ 中不同)正好是 32 位,如这里所述

回答by thejh

intis 32 bit on every platform. However, to make it unsigned, you have to manually shift numers and such stuff - it really gets complicated. Why not use a byte[] or a long?

int在每个平台上都是 32 位。但是,要使其未签名,您必须手动移动数字之类的东西 - 这真的变得很复杂。为什么不使用 byte[] 或 long?

回答by dmck

The int will be 32bits even on 64 bit machines, you don't need to do anything.

即使在 64 位机器上,int 也是 32 位,你不需要做任何事情。

From the Java language spec (http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)

来自 Java 语言规范 ( http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)

The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.

int 数据类型是一个 32 位有符号二进制补码整数。它的最小值为 -2,147,483,648,最大值为 2,147,483,647(含)。对于整数值,此数据类型通常是默认选择。对于您的程序将使用的数字,此数据类型很可能足够大,但如果您需要更大范围的值,请改用 long。

The primary difference between 32bit and 64bit java VMs is the maximum heap size, i.e. the amount of memory the VM can manage.

32 位和 64 位 java VM 之间的主要区别是最大堆大小,即 VM 可以管理的内存量。

However, the internal implementation of 64bit VMs will have an affect on memory usage:

但是,64 位 VM 的内部实现会对内存使用产生影响:

Generally, the benefits of being able to address larger amounts of memory come with a small performance loss in 64-bit VMs versus running the same application on a 32-bit VM. This is due to the fact that every native pointer in the system takes up 8 bytes instead of 4. The loading of this extra data has an impact on memory usage which translates to slightly slower execution depending on how many pointers get loaded during the execution of your Java program.

通常,与在 32 位 VM 上运行相同的应用程序相比,能够处理大量内存的好处是在 64 位 VM 中性能损失很小。这是因为系统中的每个本机指针占用 8 个字节而不是 4 个字节。加载这些额外数据会影响内存使用,这取决于在执行你的 Java 程序。

From: http://www.oracle.com/technetwork/java/hotspotfaq-138619.html#64bit_description

来自:http: //www.oracle.com/technetwork/java/hotspotfaq-138619.html#64bit_description

回答by Nikhil Kumar

In java int is 32 bit. Longs are 64 bit. So if is is greater than 32 bit you need long.

在java中int是32位。多头是 64 位。因此,如果大于 32 位,则需要很长时间。