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

In this Java program, we will learn how to find the GCD of two numbers using Java.  GCD (Greatest Common...
  • Java
  • December 3, 2024
In this Java Program, you’ll learn how to swap two numbers using the Java programming language.  How to Swap Two...
  • Java
  • December 2, 2024
In this Java program , we will learn how to Find Largest Element in an Array in your Java program.   How to Find Largest Element...
  • Java
  • December 2, 2024