Java Integer getInteger() Method

There are three different types of Java getInteger() methods which can be differentiated depending on its parameter. These are:

  1. Java Integer getInteger(String nm) Method
  2. Java Integer getInteger(String nm, int val) Method
  3. Java Integer getInteger(String nm, Integer val) Method

1. Java Integer getInteger(String nm) Method:

The getInteger(String nm) method is a method of Integer class under java.lang package. This method determines the integer value of the system property with the specified name of method argument. If there is no property with the specified name of method argument, if the specified name is empty or null or if the property does not have the correct numeric format, it will returned null.

Syntax:

Following is the declaration of getInteger(String nm) method:

Parameter:

DataTypeParameterDescriptionRequired/Optional
StringnmIt is the system property name from which we get the value as an integer object.Required

Returns:

The getInteger(String nm) method will returns the value of the property specified as method argument as an Integer object.

Exceptions:

NA

Compatibility Version:

Java 1.2 and above

Example 1

Output:

sun.arch.data.model is the Java System property which determines the version of the JVM is used - 32 or 64bit.

Value = 64
Value = null

Example 2

Output:

Default property: 64
Custom Property: 100

Example 3

Output:

Enter the desired input string: sun.arch.data.model
Default Value = 64
Custom Value = null

2. Java Integer getInteger(String nm, int val) Method:

The getInteger(String nm, int val) method is a method of Integer class under java.lang package. This method determines the integer value of the system property with the specified name of method argument.

The first argument is treated as the name of a system property. The string value of this property is then interpreted as an integer value and an Integer object representing this value is returned.

The second argument is the default value. An Integer object that represents the value of the second argument is 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.

Syntax:

Following is the declaration of getInteger(String nm, int val) method:

Parameter:

DataTypeParameterDescriptionRequired/Optional
StringnmIt is the system property name from which we get the value as an integer object.Required
IntvalIt is the default value specified by user which returned if property were not found.Required

Returns:

The getInteger(String nm, int val) method returns the value of the system property name specified as method argument as an Integer object and if not found, the default value will be returned as specified by the user.

Exceptions:

NA

Compatibility Version:

Java 1.2 and above

Example 1

Output:

102
999

Example 2

Output:

System Property: 123
Default property: 64
Custom Property:100
Custom Property: 124

Example 3

Output:

Enter the desired system property:
sun.arch.data.model
Default Value: 64
Custem Value: 25

3. Java Integer getInteger(String nm, Integer val) Method:

The getInteger( String nm, Integer val ) method is a method of Integer class under java.lang package. This method determines the integer value of the system property with the specified name of method argument.

The first argument is treated as the name of a system property. System properties are accessible through the System.getProperty(java.lang.String) method. The string value of this property is then interpreted as an integer value and an Integer object representing this value is returned.

  • If the property value begins with the two ASCII characters 0x or the ASCII character #, not followed by a minus sign, rest of it is parsed as a hexadecimal integer exactly.
  • If the property value begins with the ASCII character 0 followed by another character, rest of it is parsed as an octal integer exactly.
  • Otherwise, the property value is parsed as a decimal integer.

The second argument is the default value. This value is 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.

Syntax:

Following is the declaration of getInteger(String nm, Integer val) method:

Parameter:

DataTypeParameterDescriptionRequired/Optional
StringnmIt is the system property name from which we get the value as an integer object.Required
IntegervalIt is the deafult value specified by user which returned if property were not found.Required

Returns:

The getInteger(String nm, Integer val) method returns the value of the system property name specified as method argument as an Integer object and if not found, the default value will be returned as specified by the user.

Exceptions:

NA

Compatibility Version:

Java 1.2 and above

Example 1

Output:

133
Default property:64
Custom Property: 100
Custom Property: 105

Example 2

Output:

Enter the desired System Property name: sun.arch.data.model
System Property Value: 64
Custom Value: 25

Example 3

Output:

102
950

Next TopicPackage-class