Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Integer getInteger(String nm, int val) Method

The java getInteger(String nm, int val) method of the Integer class is used to determine the integer value that is associated with the system property passed as the string argument. The second argument val is the default value and is returned if:

  • no property of the given name exists.

  • if the property is not given the correct numeric format

  • or, the passed property is either null or empty.

Syntax:

public static Integer getInteger(String nm, int val)

Parameters:

The parameters passed are String nm whose system property's integer value is to be determined and int val which is the default value 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 value 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 value 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)
   {

     
      String s = "sun.arch.data.model";
      System.out.println(Integer.getInteger(s, 0));//returns the integer value of the system property of string s
      
      System.out.println(Integer.getInteger("java.vm.specification.vendor", 100)); // will return the default value as string as there is no property of the given name
      
      System.out.println(Integer.getInteger("studytonight",0)); // will return the default value as string does not have a system property value
   }
} 


64
100
0

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();
           int i= sc.nextInt();
           System.out.println("Default Value: "+Integer.getInteger(s, i)); //will returns the integer value of the system property 
           
        }
        catch(Exception e)
        {
          
        }
        
          
    }  
}  


Enter the value and default value
mohit 78
Default Value: 78

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.