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
Call
What 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
Parameter
Description
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 Value
Description
Integer
The 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:
decode rules apply. Because the value is decoded, "0x1F" is 31 and "#FF" is 255.
The octal surprise."010" starts with zero, so it is octal and gives 8.
Invalid values use the default. A leading space, a decimal point, and a word are not numbers, so the default -1 is returned.
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());
}
}
Output
no flag: 8080
with flag: 9090
bad flag: 8080
How It Works
A helper with a default.choosePort asks for server.port and falls back to 8080.
Nothing set. The first call finds no property and returns the default 8080.
A flag was given. After setProperty (the same effect as java -Dserver.port=9090), the call returns 9090.
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.