70

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

jon077
  • 10,303
  • 11
  • 39
  • 37

7 Answers7

85

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.

Pedro Dusso
  • 2,100
  • 9
  • 34
  • 64
Thomas Jones-Low
  • 7,001
  • 2
  • 32
  • 36
  • Providing an `int` size of 32 bit in java after compiling with 64 bit javac compiler and interpret using 64 bit jvm is a big disadvantage from application's scale enhancement perspective?Because if i have `int` type `model_number` which is a bit level concatenation of `short` type `parent_id` and `short` type `model_id`. How do i enhance `model_number` to 64 bit in existing app? using `long` type would require lot of code changes in enterprise app. Do you think it was a wrong decision to use `int` in first place? – overexchange Nov 27 '14 at 03:39
  • Short values are always 16 bits. Concatenating them will always be 32 bits (an int). So I wouldn't change the code. The performance differences between the 32 bit int and the 64 bit int on the larger platform are taken care of in the JIT compiler. – Thomas Jones-Low Nov 28 '14 at 16:12
  • i mean to say, if i change `short` to `int` type for `parent_id` and `model_id`, then i would like to think of `model_number` as 64 bit. – overexchange Nov 28 '14 at 23:58
  • I would be curious as to why you are using a concatenation in the first place rather than two member variables in the class. But yes, if you change the `parent_id` and `model_id`, you will have to change `model_number` as well. – Thomas Jones-Low Nov 30 '14 at 03:02
  • Because `model_number` sits in database for persistence. So, i need to concatenate at bit level before storing in database.My question is, How do i make `model_number` as 64 bit after, by declaring it as `long`? for that i need to change existing complete code that use 32 bit `int`. – overexchange Nov 30 '14 at 03:11
23

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.

Pedro Dusso
  • 2,100
  • 9
  • 34
  • 64
erickson
  • 265,237
  • 58
  • 395
  • 493
  • Can java source code compiled with 32 bit javac compiler, run in 64 bit jvm? Does this class file take an advantage of more memory? – overexchange Nov 27 '14 at 03:45
  • Yes. The class files are identical. It doesn't matter where compilation is done, only the runtime environment. – erickson Nov 27 '14 at 04:10
  • The link is to the [Java Language Specification](http://docs.oracle.com/javase/specs/jls/se8/html/index.html), not the [Java Virtual Machine Specification](http://docs.oracle.com/javase/specs/jvms/se8/html/index.html). – anishpatel Jun 22 '17 at 20:06
9

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

Powerlord
  • 87,612
  • 17
  • 125
  • 175
4

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.

Phil
  • 815
  • 5
  • 13
2

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 ;-)

mfx
  • 7,168
  • 26
  • 29
0

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

svpino
  • 1,864
  • 17
  • 18
0

Yup, it's 32 bits

Chad Okere
  • 4,570
  • 1
  • 21
  • 19