Java Program to Find Size of Different DataTypes

In this post, we will learn how to find the size of different data types using Java Programming language.

So, without further ado, let’s begin this tutorial.

Java Program to Find Size of Different DataTypes

// Java Program to Find Size of Different DataTypes
public class SizeOfDataTypes{
    public static void main(String[] args){
        System.out.println("Type            Size(in bytes)");
        System.out.println("Character         " + Character.BYTES);
        System.out.println("Integer           " + Integer.BYTES);
        System.out.println("Long              " + Long.BYTES);
        System.out.println("Short             " + Short.BYTES);
        System.out.println("Double            " + Double.BYTES);
        System.out.println("Float             " + Float.BYTES);
    }
}

Output

Type            Size(in bytes)
Character         2
Integer           4
Long              8
Short             2
Double            8
Float             4

How Does This Program Work ?

        System.out.println("Type            Size(in bytes)");
        System.out.println("Character         " + Character.BYTES);
        System.out.println("Integer           " + Integer.BYTES);
        System.out.println("Long              " + Long.BYTES);
        System.out.println("Short             " + Short.BYTES);
        System.out.println("Double            " + Double.BYTES);
        System.out.println("Float             " + Float.BYTES);

We use static variables BYTES of non-primitive data type to find the size of almost every data type.

Conclusion

I hope after going through this post, you understand how to find the size of different data types using Java Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published.