简体   繁体   中英

Integer range when using 64bit jdk

As I understand the difference between two integer from 32bit & 64bit are the following: 32bit range −2,147,483,648 to 2,147,483,647 64bit range: −9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

I am using a 64bit jdk, I validate it by printing the following: System.out.println("JVM Bit size: " + System.getProperty("sun.arch.data.model"));

JVM Bit size: 64

when I try to init a new Integer variable with number bigger ther 10 letters I get a compilation error. why is that? it looks like the 64bit is larger

example (ran on netbeans): int x = 12345678910; => Error: integer is too large

The size of an int in Java is completely independent of the 32-bitness or 64-bitness of a JDK. It is always 4 bytes = 32 bits = −2,147,483,648 to 2,147,483,647.

If you want a 64-bit integer, use a long , which is always 64 bits = 8 bytes.

Unlike other languages, Java's numeric primitive types are always the same size, whatever the platform (32bit or 64bit, LE or BE); they are all big endian and are 1 byte long for byte , 2 bytes long for short and char , 4 bytes long for int and 8 bytes long for long .

If it were not the case, jars would not be portable across platforms...

Your best resource is JLS :

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 UTF-16 code units

4.2.1. Integral Types and Values

The values of the integral types are integers in the following ranges:

  1. For byte, from -128 to 127, inclusive

  2. For short, from -32768 to 32767, inclusive

  3. For int, from -2147483648 to 2147483647, inclusive

  4. For long, from -9223372036854775808 to 9223372036854775807, inclusive

  5. For char, from '\' to '\￿' inclusive, that is, from 0 to 65535

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM