Java Program to print Size of all Data Types

Notes:

Java Program to print Size of all Data Types:
- Data type indicates type of data
- Data type also indicates the amount of memory to be allocated for a specific type of data

- W.K.T. There are 8 primitive types available in Java language
1. byte : 08 bits - 1 byte
2. short : 16 bits - 2 bytes
3. int : 32 bits - 4 bytes
4. long : 64 bits - 8 bytes
5. float : 32 bits - 4 bytes
6. double : 64 bits - 8 bytes
7. char : 16 bits - 2 bytes
8. boolean : 1 bit

- To find and print size of any primitive data type; we take help its associated wrapper class.
- Inside each wrapper class (except Boolean type) there is a SIZE property; which returns the amount of memory being allocated for that specific type of data in bits

Example Code:
Program to print size of all data types in Java

package sizeofdatatypesdemo;

public class SizeofDatatypesDemo {

public static void main(String[] args) {

System.out.println("Size of byte = " + Byte.SIZE + " bits"); // 8
System.out.println("Size of short = " + Short.SIZE + " bits"); // 16
System.out.println("Size of int = " + Integer.SIZE + " bits"); // 32
System.out.println("Size of long = " + Long.SIZE + " bits"); // 64
System.out.println("Size of float = " + Float.SIZE + " bits"); // 32
System.out.println("Size of double = " + Double.SIZE + " bits"); // 64
System.out.println("Size of char = " + Character.SIZE + " bits"); //16
System.out.println("Size of boolean = " + 1 + " bit"); // 1
System.out.println();

System.out.println("Size of byte = " + Byte.SIZE / 8 + " byte"); // 1
System.out.println("Size of short = " + Short.SIZE / 8 + " bytes"); // 2
System.out.println("Size of int = " + Integer.SIZE / 8 + " bytes"); // 4
System.out.println("Size of long = " + Long.SIZE / 8 + " bytes"); // 8
System.out.println("Size of float = " + Float.SIZE / 8 + " bytes"); // 4
System.out.println("Size of double = " + Double.SIZE / 8 + " bytes"); // 8
System.out.println("Size of char = " + Character.SIZE / 8 + " bytes"); // 2
System.out.println("Size of boolean = " + 1 + " bit"); // 1

}
}