Integer.getInteger() method in Java with Examples

The getInteger() is a static method of the Integer class that is used to get the Integer instance of the system property with the specified name. It has three overloaded methods –

Let’s have a look at all of them one by one.

public static Integer getInteger(String nm, Integer val)

  • What does it do? It will return the integer value of the system property that is specified by the first argument
    • The first argument is treated as the system property name and will be used to get the system property by using System.getProperty(nm)
    • If the system property exists, the value of that system property will be converted to its corresponding integer instance by using the decode() method
    • And if there is no such property, then the second argument (val) passed to the function will be returned as a default value
  • What does it return? It will return an Integer instance
Code Example
public class Codekru {

	public static void main(String[] args) {

		System.setProperty("website", "20"); // setting the "website" property
		
		Integer defaultValue  = 123;

		System.out.println("website property integer value: " + Integer.getInteger("website", defaultValue)); // will print 20

		System.out.println("codekru property integer value: " + Integer.getInteger("codekru", defaultValue)); // will print 123

	}
}

Output –

website property integer value: 20
codekru property integer value: 123

Here, “website” returned an integer value of 20 while “codekru” returned 123.

  • We have explicitly set the “website” property with a string value of “20”. So, the same is returned by the getIntger() method
  • But we haven’t set any property named “codekru”. So, the default value represented by the second argument of the getInteger() method is returned.

But, now the question is that we have set a string value (“20”) for the property “website” but have gotten an Integer value ( 20 ) after calling the getInteger() method. This is because the string value is converted into its corresponding integer instance by using the decode() method of the Integer class.

Internal Implementation of the getInteger(String nm, Integer val)
    public static Integer getInteger(String nm, Integer val) {
        String v = null;
        try {
            v = System.getProperty(nm);
        } catch (IllegalArgumentException | NullPointerException e) {
        }
        if (v != null) {
            try {
                return Integer.decode(v);
            } catch (NumberFormatException e) {
            }
        }
        return val;
    }

Here, we can see that the decode() method is used on the System.getProperty(nm) value. The decode() method converts the hexadecimal or the octal string into its corresponding integer instance. To know more about it, please look at our decode() method article, where we have explained everything related to the decode() method in detail.

public static Integer getInteger(String nm, int val)

  • What does it do? This method is the same as getInteger(String nm, Integer val), with a little difference that it accepts an int primitive instead of an Integer instance as the second argument. It will also return the integer value of the system property specified by the first argument, and if no such property exists, then the second argument will be returned as a default value
  • What does it return? It will also return an Integer instance
Code Example
public class Codekru {

	public static void main(String[] args) {

		System.setProperty("website", "20"); // setting the "website" property

		System.out.println("website property integer value: " + Integer.getInteger("website", 123)); // will print 20

		System.out.println("codekru property integer value: " + Integer.getInteger("codekru", 123)); // will print 123

	}
}

Output –

website property integer value: 20
codekru property integer value: 123
Internal Implementation of the getInteger(String nm, int val)
    public static Integer getInteger(String nm, int val) {
        Integer result = getInteger(nm, null);
        return (result == null) ? Integer.valueOf(val) : result;
    }

As we can see that getInteger(String nm, int val) internally calls the getInteger(String nm, Integer val) with Integer val = null.

And if result == null, means the system property is not found for the specified name, then getInteger(String nm, int val) will return the Integer instance of its second argument by using the valueOf() method, otherwise, it will return the result value.

public static Integer getInteger(String nm)

  • What does it do? It will also return the integer instance of the system property specified by its only argument.
  • What does it return? It will return the corresponding integer instance of the system property specified by the argument. And if that property doesn’t exist, it will return null.
Code Example
public class Codekru {

	public static void main(String[] args) {

		System.setProperty("website", "20"); // setting the "website" property

		System.out.println("website property integer value: " + Integer.getInteger("website")); // will print 20

		System.out.println("codekru property integer value: " + Integer.getInteger("codekru")); // will print 123

	}
}

Output –

website property integer value: 20
codekru property integer value: null
Internal Implementation of the getInteger(String nm)
public static Integer getInteger(String nm) {
    return getInteger(nm, null);
}

getInteger(String nm) internally uses the getInteger(String nm, Integer val) with Integer val = null.

Please visit this link to learn more about the Integer wrapper class of java and its other functions or methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *