The getInteger() method of Integer class, returns the integer value of the system property with the specified property name.
Hierarchy:
java.lang Package -> Integer Class -> getInteger() Method
Syntax of getInteger() method
There are three variations of getInteger() method. Let’s discuss them one by one. This first variation accept a single String name nm
, which represents the property name.
If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then it returns null.
public static Integer getInteger(String nm)
Second variation of this method with String and int arguments:
public static Integer getInteger(String nm, int val)
Third variation of this method with String and Integer arguments:
public static Integer getInteger(String nm, Integer val)
getInteger() Parameters
nm
– Property name.val
– Default value. This value is returned if the specified property does not exist.
getInteger() Return Value
- The Integer value of the property.
Supported java versions: Java 1.2 and onwards.
Example 1: Method getInteger(String nm)
public class JavaExample { public static void main(String[] args) { String str = "java.vendor.url"; System.out.println("Integer Value: "+Integer.getInteger(str)); str = "os.arch"; System.out.println("Integer Value: "+Integer.getInteger(str)); } }
Output:
Example 2: Method getInteger(String nm, int val)
public class JavaExample { public static void main(String[] args) { String str = "java.vendor.url"; System.out.println("Integer Value: "+Integer.getInteger(str)); System.setProperty(str, "101"); System.out.println("Integer Value: "+Integer.getInteger(str)); String str2 = "custom.property"; System.out.println("Integer Value: "+Integer.getInteger(str2, 200)); } }
Output:
Example 3: Method getInteger(String nm, Integer val)
public class JavaExample { public static void main(String[] args) { String str = "custom.property.1"; Integer i = new Integer(100); System.out.println("Integer Value: "+Integer.getInteger(str, i)); String str2 = "custom.property.2"; Integer i2 = new Integer(1000); System.out.println("Integer Value: "+Integer.getInteger(str2, i2)); } }
Output: