primitive type range

public class Main {
    public static void main(String[] args) {
        System.out.println("int type can store values from " + Integer.MIN_VALUE 
                + " to " + Integer.MAX_VALUE);
    }
}

The snippet result is

int type can store values from -2147483648 to 2147483647

If you don't know the int type value precisely, you could use Integer MIN_VALUE and MAX_VALUE to get the type value range. You could use the same method to determine the byte, short, long, float and double type value range.

System.out.println("byte type can store values from " + Byte.MIN_VALUE
                + " to " + Byte.MAX_VALUE);
System.out.println("short type can store values from " + Short.MIN_VALUE
                + " to " + Short.MAX_VALUE);
System.out.println("long type can store values from " + Long.MIN_VALUE
                + " to " + Long.MAX_VALUE);
System.out.println("float type can store values from " + Float.MIN_VALUE
                + " to " + Float.MAX_VALUE);
System.out.println("double type can store values from " + Double.MIN_VALUE
                + " to " + Double.MAX_VALUE);

The snippet result is

byte type can store values from -128 to 127

short type can store values from -32768 to 32767

long type can store values from -9223372036854775808 to 9223372036854775807

float type can store values from 1.4E-45 to 3.4028235E38

double type can store values from 4.9E-324 to 1.7976931348623157E308

// int type overflow
int higherMax = Integer.MAX_VALUE + 1;
        System.out.println("int type overflow: " + 
                Integer.MAX_VALUE + " + 1 = " + higherMax);

The snippet result is

int type overflow: 2147483647 + 1 = -2147483648

If you use some value that extends the int type value, for example, 2147483647 + 1, the int type will return -2147483648. It is called overflow. The int type could not hold a value higher than the max value or lower than the min value.

Last updated