Byte Short Int Long Range In Java

Byte Short Int Long Range In Java | There are eight different primitive data types in Java, they are char, Boolean, double, float, long, Int, byte, and short. As per the scope of this blog, we are just concentrating on the following data types: Byte, Short, Int, and Long.

The byte, short, int, and long data types are all integer data types, which means they only store the numeric value. But they have differences in size and range. Each primitive data type requires a different amount of memory and also few specific operations can be performed. The byte data types take the shortest memory space whereas the long data types take the largest memory space.

Range Of Byte Int Short Long In Java

Below we have mentioned the data types in ascending order where the byte takes the least space and the long occupies the largest space.

Byte < short < int < long

The same thing can be written in descending order as:-

Long > int > short > byte

So as per the above comparison, the byte occupies the least memory, then the short occupies more space than the byte then the int occupies more space than the short, and at last long occupies more space than the int. So from this, we can say that if we want to store a large numeric value long would be the best data type, and if we want to store a lesser value then byte would be the best data type.

Byte Short Int Long Range In Java

Data TypeSizeRange
Byte8 bits-128 to 127
Short16 bits-32,768 to 32767
Int32 bits-2,147,483,648 to 2,147,483647
Long64 bits–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Java Program to Find the Range of Byte, Short, Int, and Long

Java provided wrapper classes for every primitive data type to store the primitive value as an object. Those wrapper classes contain MIN_VALUE and MAX_VALUE static variables holding the minimum and maximum minimum and maximum ranges.

Java Program to Find Byte Short Int Long Range

public class Main {
    public static void main(String[] arg) {
        System.out.println("The range of byte is from: "
                + Byte.MIN_VALUE + " to: " + Byte.MAX_VALUE);
        System.out.println("The range of short is from: "
                + Short.MIN_VALUE + " to: " + Short.MAX_VALUE);
        System.out.println("The range of int is from: "
                + Integer.MIN_VALUE + " to: " + Integer.MAX_VALUE);
        System.out.println("The range of long is from: "
                + Long.MIN_VALUE + " to: " + Long.MAX_VALUE);
    }
}

Output:-

The range of byte is from: -128 to: 127
The range of short is from: -32768 to: 32767
The range of Int is from: -2147483648 to: 2147483647
The range of long is from: -9223372036854775808 to: 9223372036854775807

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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