Getting size of Primitive data types – Computer Sir Ki Class


Code Learning #JAVA#565 siteicon   siteicon   siteicon  

Getting size of Primitive data types

Outputs the size of the eight primitive data types present in java.

Learning Objectives

To learn java concepts related to :

  • The size of each primitive data type.
  • The function wrapperclass.SIZE.

Program Approach

The objective is just to get the value of the space allotted to each data type.

Since we need to get the size of each data type we will use the method wrapper class.SIZE. Wrapper classes have the same names as data types but the first letter of each wrapper class is in upper case. For eg:-The wrapper class of integer is Integer.

Source Code

TC++ #565

Source Code

class SizeOfPrimitives
public static void main (String[] args)
System.out.println("Size of char: " + (Character.SIZE) + " bit");
System.out.println("Size of byte: " + (Byte.SIZE) + " bit");
System.out.println("Size of short: " + (Short.SIZE) + " bit");
System.out.println("Size of int: " + (Integer.SIZE) + " bit ");
System.out.println("Size of long: " + (Long.SIZE) + " bit");
System.out.println("Size of float: " + (Float.SIZE) + " bit");
System.out.println("Size of double: " + (Double.SIZE) + " bit");

Test it !

Run Output

Size of char:16 bit
Size of byte:8 bit
Size of short:16 bit
Size of int:32 bit
Size of long:64 bit
Size of float:32 bit
Size of double:64 bit

Code Understanding

System.out.println(“Size of char: ” + (Character.SIZE) + ” bit”);
System.out.println(“Size of byte: ” + (Byte.SIZE) + ” bit”);
System.out.println(“Size of short: ” + (Short.SIZE) + ” bit”);
System.out.println(“Size of int: ” + (Integer.SIZE) + ” bit “);
System.out.println(“Size of long: ” + (Long.SIZE) + ” bit”);
System.out.println(“Size of float: ” + (Float.SIZE) + ” bit”);
System.out.println(“Size of double: ” + (Double.SIZE) + ” bit”);

Here we have used the  wrapper classes corresponding to each primitive data type. Wrapper class names are simply Capitalized first name for corresponding data type. For e.g. Char for char, Byte for byte, Int for int and so on.





The concept of wrapper class makes it possible to use various methods and properties of a class to perform various actions. For example the SIZE property determines the memory space required. This concept we shall understand more when we will study classes and wrapper classes of primitive data types.

Suggested Filename(s):


admin| Created: 16-Aug-2016 | Updated: 21-Mar-2018|