Java Integer getInteger() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

In Java, Integer.getInteger(name) looks up the system property called name and returns its value as an Integer. System properties are the settings that you pass on the command line with -Dname=value or set in code with System.setProperty().

If the property does not exist, or its value is not a valid number, you get null, or the default value that you passed as a second argument. It does not read environment variables or text files.

A static method of java.lang.Integer: call it on the class, as Integer.getInteger(...). Integer lives in java.lang, so no import is needed.

Syntax

Pass the property name, and optionally a default value.

Java
// java -Dserver.port=9090 App
Integer a = Integer.getInteger("server.port");        // 9090, or null if unset
Integer b = Integer.getInteger("server.port", 8080);  // 9090, or 8080 if unset
CallWhat it does
Integer.getInteger(name)The property as an Integer, or null.
Integer.getInteger(name, 8080)The property, or an Integer holding the int default.
Integer.getInteger(name, (Integer) null)The property, or null as the default.
System.getProperty(name)The raw text of the property, with no conversion.

The value is read with decode, so "0x10" is 16 and "010" is octal 8. Spaces make the value invalid.

Library declarations
Java
// java.lang.Integer
public static Integer getInteger(String nm)
public static Integer getInteger(String nm, int val)
public static Integer getInteger(String nm, Integer val)

Parameters

ParameterDescription
nm (String)The property name, for example "server.port". A null or empty name gives the default.
val (int or Integer)The value to return when the property is missing or not a valid number. Optional.

Return Value

Return ValueDescription
IntegerThe decoded property value, or the default. The one-argument form returns null when nothing usable is found.

Compare with Similar Methods

Other ways to get a number from outside the program:

MethodHow it differs
decode(s)The parsing rules that getInteger applies to the property value.
parseInt(s)Plain parsing of text you already hold, for example from System.getProperty().
valueOf(s)Parses text and returns an Integer, but never reads properties.
System.getenv(name)Reads operating-system environment variables instead.

Java Examples

Example 1 — Read Settings from System Properties

Set a few properties in code, then read them back with and without a default.

Java
public class IntegerGetIntegerDemo {
    public static void main(String[] args) {
        System.setProperty("app.threads", "4");

        Integer threads = Integer.getInteger("app.threads");
        System.out.println("app.threads: " + threads);

        Integer retries = Integer.getInteger("app.retries");
        System.out.println("app.retries: " + retries);

        int retriesOrDefault = Integer.getInteger("app.retries", 3);
        System.out.println("with default: " + retriesOrDefault);

        try {
            int bad = Integer.getInteger("app.retries");
            System.out.println(bad);
        } catch (NullPointerException e) {
            System.out.println("unboxing null -> NullPointerException");
        }
    }
}

How It Works

  1. Set, then read. setProperty stores the text "4", and getInteger converts it into the Integer 4.
  2. Missing means null. Nobody set app.retries, so the one-argument form returns null, which prints as the word null.
  3. A default fills the gap. The two-argument form returns the default 3 when the property is missing.
  4. The trap. Putting the null result into an int forces unboxing, which throws NullPointerException.

Example 2 — See Which Property Values Are Accepted

Store different kinds of text in properties and see which ones getInteger understands.

Java
public class IntegerGetIntegerFormats {
    public static void main(String[] args) {
        System.setProperty("p.plain", "25");
        System.setProperty("p.hex", "0x1F");
        System.setProperty("p.colour", "#FF");
        System.setProperty("p.octal", "010");
        System.setProperty("p.spaced", " 5");
        System.setProperty("p.decimal", "12.5");
        System.setProperty("p.word", "many");

        String[] names = {"p.plain", "p.hex", "p.colour", "p.octal", "p.spaced", "p.decimal", "p.word"};
        for (String name : names) {
            System.out.println(name + " -> " + Integer.getInteger(name, -1));
        }
    }
}

How It Works

  1. decode rules apply. Because the value is decoded, "0x1F" is 31 and "#FF" is 255.
  2. The octal surprise. "010" starts with zero, so it is octal and gives 8.
  3. Invalid values use the default. A leading space, a decimal point, and a word are not numbers, so the default -1 is returned.
  4. No exception. Unlike parseInt, this method hides the NumberFormatException and quietly falls back.

Example 3 — Choose a Server Port with a Fallback

Use a default so the program works with or without a -Dserver.port flag.

Java
public class IntegerGetIntegerPort {
    static int choosePort() {
        return Integer.getInteger("server.port", 8080);
    }

    public static void main(String[] args) {
        System.out.println("no flag:   " + choosePort());

        System.setProperty("server.port", "9090");
        System.out.println("with flag: " + choosePort());

        System.setProperty("server.port", "not-a-port");
        System.out.println("bad flag:  " + choosePort());
    }
}

How It Works

  1. A helper with a default. choosePort asks for server.port and falls back to 8080.
  2. Nothing set. The first call finds no property and returns the default 8080.
  3. A flag was given. After setProperty (the same effect as java -Dserver.port=9090), the call returns 9090.
  4. Garbage is ignored. The value "not-a-port" cannot be decoded, so the default returns again.

Frequently Asked Questions

It reads a system property by name and returns its value as an Integer, or null (or your default) when the property is missing or not a valid number.
No. It reads only Java system properties, such as those set with -Dname=value or System.setProperty(). Use System.getenv() for environment variables.
Start the program with -D, for example java -Dserver.port=9090 App. Then Integer.getInteger("server.port") returns 9090.
The one-argument form returns null for a missing property, and assigning null to an int unboxes it. Pass a default, or store the result in an Integer and check for null.
No. parseInt converts text you already have and throws on bad input. getInteger first looks the text up in system properties and returns a default instead of throwing.

Next: hashCode()

Get the hash code of an Integer (its own value).

hashCode() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful