(toc)

The Integer.getInteger() method in Java is used to retrieve the value of a system property as an integer. It is a static method of the Integer class and is part of the java.lang package.

Unlike other methods of the Integer class, getInteger() does not directly convert strings to integers. Instead, it looks for a system property with the specified name, retrieves its value as a string, and converts it to an integer.


Syntax


public static Integer getInteger(String propertyName) public static Integer getInteger(String propertyName, int defaultValue) public static Integer getInteger(String propertyName, Integer defaultValue)

Parameters

  1. propertyName: The name of the system property to look up.
  2. defaultValue (optional): The default value to return if the property is not found or cannot be parsed as an integer.

Returns

  • If the system property is found and can be parsed as an integer, it returns the integer value.
  • If the property is not found or cannot be parsed, it returns the default value (if provided) or null (if no default value is provided).

Examples

Example 1: Retrieve a System Property


public class GetIntegerExample1 { public static void main(String[] args) { // Set a system property System.setProperty("example.property", "42"); // Retrieve the property as an integer Integer value = Integer.getInteger("example.property"); System.out.println("Value: " + value); // Output: Value: 42 } }

Example 2: Use a Default Value


public class GetIntegerExample2 { public static void main(String[] args) { // Retrieve a non-existent property with a default value Integer value = Integer.getInteger("nonexistent.property", 100); System.out.println("Value: " + value); // Output: Value: 100 } }

Example 3: Property Not an Integer


public class GetIntegerExample3 { public static void main(String[] args) { // Set a system property with a non-integer value System.setProperty("example.property", "notAnInteger"); // Retrieve the property as an integer Integer value = Integer.getInteger("example.property", 0); System.out.println("Value: " + value); // Output: Value: 0 } }

Practical Use Case

The getInteger() method is typically used to configure applications by reading system properties, such as timeouts or buffer sizes, as integers.

Example: Configuring Application Parameters

public class ApplicationConfig {
public static void main(String[] args) { // Default timeout is 30 seconds int timeout = Integer.getInteger("app.timeout", 30); System.out.println("Application timeout: " + timeout + " seconds"); } }

You can run the program with the -D flag to set the property:


java -Dapp.timeout=60 ApplicationConfig

Output:
Application timeout: 60 seconds


Key Points

  1. Null Return Value: If no default value is provided and the property is not found, getInteger() returns null.
  2. Non-Integer Values: If the system property cannot be parsed as an integer, the method returns the default value.
  3. Default Value: A fallback mechanism ensures robustness in scenarios where the property might not exist.

Difference Between getInteger() and parseInt()

Aspect Integer.getInteger() Integer.parseInt()
Purpose Reads system properties and converts them to integers. Converts a string directly to an integer.
Input Name of a system property. String containing a numeric value.
Default Value Allows a default value. No default value; throws NumberFormatException.

Leave a Reply