java.lang.Integer getInteger(String nm, int val)

Description :

This java tutorial shows how to use the getInteger(String nm, int val) method of Integer class under java.lang package. This method returns the Integer value of System.property that has been specified as method argument or if the property has not been found then the val specified will be returned instead.

Method Syntax :

public static Integer getInteger(String nm, int value)

Parameter Input :

 

DataType Parameter Description
String nm the system property key that we are interested in getting the value as an Integer object.
int val This is the value returned if the property were not found.

 

Method Returns :

The getString(String nm, int val) method simply returns the value of the property key specified as method argument interpreted as an Integer object and if not found the default value will be returned as determined by the val method argument.

Compatibility Version :

Requires Java 1.0 and up

Exception :

N/A

Discussion :

The getInteger(String nm, int val) method is static thus we should invoke it statically for example Integer.getInteger(String nm). This method simply returns the value of the property String specified as method argument and if the property were not found the val specified on the method argument will be returned. The method argument val served as a default value. Normally if we get the one of the system property we would use System.getProperty(String key) and it would return a String value which corresponds to the key that is passed as method argument. On the getInteger(String nm,int val) we do the same passing the String key value however the only difference is that this static method of Integer class interpret the result value to an Integer object. If the property value cannot be interpreted as an Integer, the getInteger(String nm) method returns the val specified as method argument.

Make a note that there is an overloaded method which is the getInteger(string nm). One way they would yield the same result if the val method parameter is null. Thus the following expression is true:

Integer.getInteger(String nm) = Integer.getInteger(String nm, null);

Java Code Example :

This java example source code demonstrates the use of getInteger(String nm, int val) method of Integer class. Basically we get one value of System property to demonstrate the use of this method. And also we also set one custom property and then we get the same value using the Integer.getString(String nm, int val) for us to further understand the behavior of this static method.

package com.javatutorialhq.java.examples;

import static java.lang.System.*;

/*
 * This example source code demonstrates the use of 
 * getInteger(String nm, int val) method of Integer class.
 * Basically we check for int value of one of the custom property
 * and also the custom property that we have set
 */

public class IntegerGetIntegerString {

	public static void main(String[] args) {
		// Get one default system property
		out.println(Integer.getInteger("java.vm.specification.vendor", 123));
		// without default value
		out.println("Default property:"
				+ Integer.getInteger("sun.arch.data.model"));
		// set a custom property
		System.setProperty("test.integer", "100");
		// get the value of the custom property we set
		out.println("Custom Property:" + Integer.getInteger("test.integer"));
		out.println("Custom Property:" + Integer.getInteger("none.existent",124));		

	}

}

Sample Output :

Running the getInteger(String nm, int val) method example source code of Integer class will give you the following output.

java integer getInteger(string nm, int val) method example

java integer getInteger(string nm, int val) method example

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :