In this Java program, we’ll learn how to find the size of primitive data types in your Java program.
How to Find the Size of Primitive Data Types in Java?
RUN CODE SNIPPETclass Main { public static void main (String[] args) { System.out.println("Size of byte: " + (Byte.SIZE/8) + " byte."); System.out.println("Size of short: " + (Short.SIZE/8) + " bytes."); System.out.println("Size of int: " + (Integer.SIZE/8) + " bytes."); System.out.println("Size of long: " + (Long.SIZE/8) + " bytes."); System.out.println("Size of char: " + (Character.SIZE/8) + " bytes."); System.out.println("Size of float: " + (Float.SIZE/8) + " bytes."); System.out.println("Size of double: " + (Double.SIZE/8) + " bytes."); } }
OUTPUT
Size of byte: 1 byte. Size of short: 2 bytes. Size of int: 4 bytes. Size of long: 8 bytes. Size of char: 2 bytes. Size of float: 4 bytes. Size of double: 8 bytes.
There is no size of operator to find the size of primitive data types. Except Boolean, we can find the byte constant of all primitive wrapper classes by dividing the size constant bits by eight.
The size of the primitive data types is always the same.