Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Integer.parseInt — converting text to int

Integer.parseInt(String) converts a decimal integer representation to int. Invalid text or an out-of-range value causes NumberFormatException.

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

Syntax

public static int parseInt(String s) throws NumberFormatException

Integer.parseInt(String) is a static method returning a primitive int, not an Integer object. It interprets decimal text; an optional leading ASCII + or - must precede digits.

The accepted range is -2147483648 through 2147483647. null, empty text, signs alone, whitespace, fractions, and out-of-range values cause NumberFormatException. The method does not trim whitespace.

Integer.parseInt(String, int) accepts a radix from 2 through 36; the single-argument variant uses 10. By contrast, Integer.valueOf(String) returns an Integer object.

Examples

public class Main {
    public static void main(String[] args) {
        System.out.println(Integer.parseInt("+42"));
        System.out.println(Integer.parseInt("-7"));
        try {
            Integer.parseInt("2147483648");
        } catch (NumberFormatException e) {
            System.out.println("NumberFormatException");
        }
    }
}

Result

42
-7
NumberFormatException

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