Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

int limits: Integer.MIN_VALUE and Integer.MAX_VALUE

Integer.MIN_VALUE and Integer.MAX_VALUE define the lower and upper bounds of Java's int type. Learn their values and the difference between int overflow and a calculation performed as long.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

Integer.MIN_VALUE
Integer.MAX_VALUE

The int type is a signed 32-bit integer. Integer.MIN_VALUE is −2³¹, or -2147483648, and Integer.MAX_VALUE is 2³¹ − 1, or 2147483647. Both bounds are inclusive; there is one more negative value than positive values.

Both fields of Integer are declared public static final int: they are public class constants of primitive type int, not Integer objects. Access them through the class name, without creating an instance or adding method-call parentheses.

The constants describe the bounds but do not prevent overflow. Adding 1 to the maximum value using int arithmetic produces the minimum value, without an exception. To obtain 2147483648, use (long) max + 1: conversion must happen before addition. Writing (long) (max + 1) converts the already-overflowed result instead.

The example prints both bounds, the overflow result, and the result of addition using long, in that order. The scope is Java SE 21; the production runtime version has not been attested.

Examples

public class IntegerLimitsDemo {
    public static void main(String[] args) {
        int min = Integer.MIN_VALUE;
        int max = Integer.MAX_VALUE;
        System.out.println(min);
        System.out.println(max);
        System.out.println(max + 1);
        System.out.println((long) max + 1);
    }
}

Result

-2147483648
2147483647
-2147483648
2147483648

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources