In this Java tutorial, you will learn about Integer.getInteger(String), Integer.getInteger(String, int) and Integer.getInteger(String, Integer) methods, and how to use these methods to get the integer value of the specified System Property, with the help of examples.

getInteger(String nm)

Integer.getInteger(String nm) determines the integer value of the system property with the specified name nm.

System properties are accessible through System.getProperty(String) method.

Integer.getInteger(String) returns null if

  • there is no property with the specified name.
  • the specified name is empty or null. Or
  • the property does not have the correct numeric format.

Syntax

The syntax of getInteger() method is

Integer.getInteger(String nm)

where

ParameterDescription
nmThe system property name.

Returns

The method returns object of type Integer.

ADVERTISEMENT

Example 1 – Get integer value from given string

In this example, we will take a System property "sun.arch.data.model" and find its numeric value using Integer.getInteger() method.

Java Program

public class Example {
	public static void main(String[] args){
		String str = "sun.arch.data.model";  
		Integer result = Integer.getInteger(str);
		System.out.println("Result of getInteger(\""+str+"\") = " + result);
	}
}

Output

Result of getInteger("sun.arch.data.model") = 64

Example 2 – getInteger(“x”)

In this example, we will take a System property "x" and find its numeric value using Integer.getInteger() method. Since, there is neither system property nor is set by user, getInteger() returns null.

Java Program

public class Example {
	public static void main(String[] args){
		String str = "x";  
		Integer result = Integer.getInteger(str);
		System.out.println("Result of getInteger(\""+str+"\") = " + result);
	}
}

Output

Result of getInteger("sun.arch.data.model") = null

getInteger(String nm, int val)

Integer.getInteger(String nm, int val) determines the integer value of the system property with the specified name nm. The method returns val if getInteger(String nm) returns null value for given system property name nm.

Syntax

The syntax of getInteger() method is

getInteger(String nm, int val)

where

ParameterDescription
nmThe system property name.
valThe default value to be returned, if there is no property of the specified name, if the property does not have the correct numeric format, or if the specified name is empty or null.

Returns

The method returns object of type Integer.

Example 3 – getInteger(String nm, int val)

In this example, we will take a System property "x" and find its numeric value using Integer.getInteger() method. Since, there is neither system property nor is set by user, getInteger() returns the specified default value of 20.

Java Program

public class Example {
	public static void main(String[] args){
		String nm = "x";
		int val = 20;
		Integer result = Integer.getInteger(nm, val);
		System.out.println("Result of getInteger(\""+nm+"\", "+val+") = " + result);
	}
}

Output

Result of getInteger("x", 20) = 20

Example 4 – getInteger(String nm, int val)

In this example, we will take a System property "sun.arch.data.model" and find its numeric value using Integer.getInteger() method. Since the system property is present and has a valid numeric value, default is not used.

Java Program

public class Example {
	public static void main(String[] args){
		String nm = "sun.arch.data.model";
		int val = 20;
		Integer result = Integer.getInteger(nm, val);
		System.out.println("Result of getInteger(\""+nm+"\", "+val+") = " + result);
	}
}

Output

Result of getInteger("sun.arch.data.model", 20) = 64

getInteger(String nm, Integer val)

Integer.getInteger() returns the integer value of the system property with the specified name.

Syntax

The syntax of getInteger() method is

getInteger(String nm, Integer val)

where

ParameterDescription
nmThe system property value.
valThe default value to be returned, if there is no property of the specified name, if the property does not have the correct numeric format, or if the specified name is empty or null.

Returns

The method returns object of type Integer.

Example 5 – getInteger(String nm, Integer val)

In this example, we will take a System property "x" and find its numeric value using Integer.getInteger() method. Since, there is neither system property nor is set by user, getInteger() returns the specified default Integer object val.

Java Program

public class Example {
	public static void main(String[] args){
		String nm = "x";
		Integer val = 20;
		Integer result = Integer.getInteger(nm, val);
		System.out.println("Result of getInteger(\""+nm+"\", "+val+") = " + result);
	}
}

Output

Result of getInteger("x", 20) = 20

Conclusion

In this Java Tutorial, we have learnt the syntax of getInteger() methods, and also how to use this method with the help of Java example programs.