Size of Primitive Types in Java

      No Comments on Size of Primitive Types in Java




Wrapper classes of each of the primitive types have a field “SIZE” which is public , static and final. It is the size of the primitive type of each of these corresponding wrapper classes.

Sizes of Primitive Types:

Size of each of the primitive types are as below:

byte – 1 byte
short – 2 byte
char – 2 byte
int – 4 bytes
long – 8 bytes
float – 4 bytes
double – 8 bytes

Demo Program to find Size of Primitive Types:

package com.masterjavatutorial;

public class PrimitiveSizeDemo {

	public static void main(String[] args) {

		 int sizeOfByte = Byte.SIZE;
		 System.out.println("Size of a byte in bits = "+sizeOfByte); 
		 
		 int sizeOfShort = Short.SIZE; 
		 System.out.println("Size of a short in bits = "+sizeOfShort);
		 
		 int sizeOfChar = Character.SIZE; 
		 System.out.println("Size of a char in bits = "+sizeOfChar);

		 int sizeOfInt = Integer.SIZE; 
		 System.out.println("Size of a int in bits = "+sizeOfInt);

		 int sizeOfLong = Long.SIZE; 
		 System.out.println("Size of a long in bits = "+sizeOfLong);

		 int sizeOfFloat = Float.SIZE; 
		 System.out.println("Size of a float in bits = "+sizeOfFloat);

		 int sizeOfDouble = Double.SIZE; 
		 System.out.println("Size of a double in bits = "+sizeOfDouble);

	}

}

Output of the above program is as below:

Size of a byte in bits = 8
Size of a short in bits = 16
Size of a char in bits = 16
Size of a int in bits = 32
Size of a long in bits = 64
Size of a float in bits = 32
Size of a double in bits = 64

NOTE : 1 byte = 8 bits