This program shows how you can print the size of any data type available in Java language. Unlike C and C++ here we don’t have any built-in operator like sizeof to calculate the size of the data type. But we can use static variable BYTES of Non-primitive data type to find the size for almost every data type.
Program
public class SizeOfDataTypes { public static void main(String[] args) { System.out.println("Type Size (bytes)"); System.out.println("Character " + Character.BYTES); System.out.println("Byte " + Byte.BYTES); System.out.println("Integer " + Integer.BYTES); System.out.println("Long " + Long.BYTES); System.out.println("Short " + Short.BYTES); System.out.println("Double " + Double.BYTES); System.out.println("Float " + Float.BYTES); } }
Output
Type Size (bytes) Character 2 Byte 1 Integer 4 Long 8 Short 2 Double 8 Float 4