Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

Integer.MAX_VALUE

The largest int value; its partner is Integer.MIN_VALUE.

Available since: Java 1.0

Syntax

static final int MAX_VALUE = 2147483647

Examples

The limits of integer types:

void main() {
    IO.println(Integer.MAX_VALUE);
    IO.println(Integer.MIN_VALUE);
    IO.println(Long.MAX_VALUE);
}

Output:

2147483647
-2147483648
9223372036854775807

Overflow: after the maximum comes the minimum, with no error:

void main() {
    int big = Integer.MAX_VALUE;
    IO.println(big + 1);
    try {
        Math.addExact(big, 1);
    } catch (ArithmeticException e) {
        IO.println(e.getMessage());
    }
}

Output:

-2147483648
integer overflow

Notes

An int holds numbers up to about 2.1 billion. For larger values use long (up to 9.2·10^18) or BigInteger. Math.addExact() and Math.multiplyExact() throw an exception on overflow instead of silently wrapping around. Long, Short, Byte and Double have the same constants.

See also

  • Long.parseLong() — Converts a string with an integer to a long — for numbers that do not fit in an int.
  • Math.abs() — Returns the absolute value of a number — the value without a minus sign.
  • BigInteger — An integer of any size — for when even long is not enough.

Related recipes

In the tutorial

  • Primitive types — Java's eight primitive types: int, long, double, float, char, boolean, byte and short, their ranges and overflow.

Thanks for reading!

Donate to the site — we work hard for you.