Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

int — 32-bit signed integer type

int is a primitive signed integer type in Java SE 21. Its range is fixed; integer division normally discards the fractional part, and overflow does not throw an exception.

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

Syntax

int name = expression;

int is a reserved keyword naming a primitive, signed 32-bit two's-complement integer type. Its range is -2147483648 through 2147483647, inclusive.

The declaration int count = 7; creates a variable holding an integer value. int is not the wrapper class Integer and cannot hold null. Fields and array elements of type int default to 0; local variables require assignment before reading.

Integer division rounds toward zero: 7 / 2 produces 3. The overflow case is Integer.MIN_VALUE / -1: the mathematical quotient is outside the range of int, so the result is Integer.MIN_VALUE, and no exception is thrown. Division by zero throws ArithmeticException. Addition overflow does not throw an exception: 2147483647 + 1 produces -2147483648.

To perform addition as long, widen an operand before calculating: (long) limit + 1. Merely assigning an already overflowed result to long does not repair the value.

Examples

public class IntDemo {
    public static void main(String[] args) {
        int count = 7;
        int limit = 2_147_483_647;
        System.out.println(count / 2);
        System.out.println(limit + 1);
        System.out.println((long) limit + 1);
    }
}

Result

3
-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