The int
data type is a primitive data type in Java. The size of int is fixed as defined by Java language specification. The size of an int is 32 bits and it is consistent across all platforms that support Java language.
Determining the Size of an int
We can confirm the size of an int using a simple piece of code.
public class IntSize {
public static void main(String[] args) {
System.out.println("Size of int: " + (Integer.SIZE / 8) + " bytes");
}
}
Explanation
Integer.SIZE
returns the number of bits required to represent anint
.- Since 8 bits equal to 1 bytes, dividing the retuned value by 8 converts the size from bits to bytes.
Output
The program produces following output
Size of int: 4 bytes
This confirms that the size of int
in Java is 4 bytes.
Leave a Reply