LAST UPDATED: AUGUST 27, 2020
Java Integer getInteger(String nm, Integer val) Method
Java getInteger(String, Integer)
method of the Integer
class is also used to determine the integer value that is associated with the system property passed as the string argument. The second argument here is also the default but rather than the default value a default Integer object is returned.
Syntax:
public static Integer getInteger(String nm, Integer val)
Parameters:
The parameters passed are String nm
whose system property's integer value is to be determined and Integer val
which is the default Integer object which will be returned if no system property is found.
Returns:
The integer value associated with the system property of the passed string or default Integer object if there is no system property associated with the string.
Example 1:
Here, for string s the integer value of the system property is returned and the default Integer object is returned for the string which doesn't have a system property value or name.
import java.lang.Integer;
public class StudyTonight
{
public static void main(String[] args)
{
System.out.println(Integer.getInteger("java.vm.specification.vendor", 25));// Print default system property
System.out.println("Default value is:"+ Integer.getInteger("sun.arch.data.model")); //Prints the system property
// setting and printing the custom property
System.setProperty("studytonight.java", "80");
System.out.println("Custom Property: " + Integer.getInteger("studytonight.java"));
//printing a custom property which is not set will yield default value
System.out.println("Custom Property: " + Integer.getInteger("mohit", 90));
}
}
25
Default value is:64
Custom Property: 80
Custom Property: 90
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.println("Enter the value and default value ");
Scanner sc = new Scanner(System.in);
String s = sc.next();
Integer i= sc.nextInt(); //Integer object for default value
System.out.println("Value is : "+Integer.getInteger(s, i)); //will returns the integer value of the system property
}
catch(Exception e)
{
}
}
}
Enter the value and default value
mohit 23
Value is : 23
************************************
Enter the value and default value
sun.arch.model.data 87
Value is : 87
Live Example:
Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.