On a 64-bit machine is the size of an int in Java 32 bits or 64 bits?

Java

Java Problem Overview


On a 64-bit machine is the size of an int in Java 32 bits or 64 bits?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

If you want a 64-bit integer, use a long.

Solution 4 - Java

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.

Solution 5 - Java

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.

This independence guarantee works a lot better than the attempt to abstract the OS away ;-)

Solution 6 - Java

Yup, it's 32 bits

Solution 7 - Java

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

Here is a full article on the matter:

Integral data types in Java and C#.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjon077View Question on Stackoverflow
Solution 1 - JavaThomas Jones-LowView Answer on Stackoverflow
Solution 2 - JavaericksonView Answer on Stackoverflow
Solution 3 - JavaPowerlordView Answer on Stackoverflow
Solution 4 - JavaPhilView Answer on Stackoverflow
Solution 5 - JavamfxView Answer on Stackoverflow
Solution 6 - JavaChad OkereView Answer on Stackoverflow
Solution 7 - JavasvpinoView Answer on Stackoverflow