Hi I am learning Java basics. I need to check the size of primitive data types like using sizeof();
in c language.
Example code:
int i;
System.out.print(sizeof(i));
Use the BYTES
constants (since Java 8) in the corresponding boxed classes:
sizeof(int) -> Integer.BYTES
sizeof(long) -> Long.BYTES
sizeof(char) -> Character.BYTES
sizeof(short) -> Short.BYTES
sizeof(float) -> Float.BYTES
sizeof(double) -> Double.BYTES
sizeof(byte) -> Byte.BYTES
Note that reference and boolean sizes are not defined, it's implementation-specific. In general you rarely should care about their size.
There's no need for such a thing in Java since the sizes of the primitives are set for all JVMs (unlike C where they can vary).
char
is 16 bit (actually an unsigned quantity), int
is 32 bit, long
is 64 bit.
boolean
is the ugly sister in all this. Internally it is manipulated as a 32 bit int
, but arrays of boolean
s use 1 byte per element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With