Java Program to Find the Size of Primitive Data Types

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 SNIPPET
class 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. 

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Java is a popular programming language that is used to develop a wide range of applications. If you are a...
  • Java
  • March 6, 2023
Java is a programming language and computing platform that is used to create various types of applications. It is used...
  • Java
  • March 6, 2023
In this post, you’ll learn how to download and install JUnit in Eclipse so that you can use in your...
  • Java
  • October 9, 2022