Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 351
15412s Avatar asked Jul 09 '13 16:07

15412s


2 Answers

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...

like image 166
fge Avatar answered Oct 28 '22 23:10

fge


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.

like image 29
Louis Wasserman Avatar answered Oct 28 '22 21:10

Louis Wasserman