Java Integer getInteger() Method

Java Integer getInteger() Method | Here, we are going to discuss the getInteger() method of the Integer wrapper class. The getInteger() method is used to get the integer value of the system properties with the specified name of the method argument. If there is no specified name or the argument or if the specified name is not valid then the getInteger() method throws null.

Java Integer getInteger()

There are three variations of the getInteger() method, which are as follows:-

  1. public static Integer getInteger(String string)
  2. public static Integer getInteger(String string, int value)
  3. public static Integer getInteger(String string, Integer value )

Parameters:
string: the string is the system property, the name which we the value of the Integer object.
value: it is the default value specified by the user, which is taken by the user. This value is returned if the property is not found.

Java Integer getInteger() Method Example-1

Here we are using the first variation:- public static Integer getInteger(String string)

public class Main {
    public static void main(String[] args) {
        String string = "Java";
        System.out.println("The value of string is: " + Integer.getInteger(string));
        string = "sun.arch.data.model";
        System.out.println("The value of string is: " + Integer.getInteger(string));
    }
}

Output:-

The value of string is: null
The value of string is: 64

public class Main {
    public static void main(String[] args) {
        String string = "People";
        System.setProperty(string, "100");
        System.out.println(Integer.getInteger(string));
    }
}

Output:-

100

Java Integer getInteger() Method Example-2

Here we are using the second variation:- public static Integer getInteger(String string, int value)

public class Main {
    public static void main(String[] args) {
        System.out.println(Integer.getInteger("Not Exsists", 888));
        System.out.println(Integer.getInteger("KnowProgram", 999));
    }
}

Output:-

888
999

Java Integer getInteger() Method Example-3

Here we are using the third variation:- public static Integer getInteger(String string, Integer value ).

public class Main {
    public static void main(String[] args) {
        System.out.println(
            Integer.getInteger("java.vm.specification.vendor", Integer.valueOf(173)));
    }
}

Output:-

173

public class Main {
    public static void main(String[] args) {
        System.out.print("The Custom Property of the given string: ");
        System.out.println(Integer.getInteger("no.exist", Integer.valueOf(101)));
    }
}

Output:-

The Custom Property of the given string: 101

public class Main {
    public static void main(String[] args) {
        System.out.println("The Default property of the given string: " + 
             Integer.getInteger("sun.arch.data.model"));
        System.setProperty("main.integer", "144");
        System.out.println("The Custom Property of the given string: " + 
             Integer.getInteger("main.integer"));
    }
}

Output:-

The Default property of the given string: 64
The Custom Property of the given string: 144

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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