-4

According to definition

The java.lang.Integer.getInteger(String nm) method determines the integer value of the system property with the specified name.

Can someone describe me the definition in simple terms. Where is Integer.getInteger needed. Where can i use it. Why does the below program prints different outputs.

   String str1 = "sun.arch.data.model";
   System.out.println(Integer.getInteger(str1,5));
   System.out.println(Integer.getInteger(str1));

   String str2 = "com.samples.data.model";
   System.out.println(Integer.getInteger(str2,5));
   System.out.println(Integer.getInteger(str2));

Output: 32 32 5 null

niks
  • 1,063
  • 2
  • 9
  • 18

1 Answers1

3

The Integer.getInteger(String) method states the following:

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 using the grammar supported by decode and an Integer object representing this value is returned.

If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null is returned.

The other method, with two parameters, has a default value that is returned instead of null, if the parameter is not set or is an invalid integer.

Since sun.arch.data.model is a valid property with a numeric value, its value is returned both times (it is 32). com.samples.data.model is either non-existent or non-numeric and so the invalid handling logic is invoked instead, returning the default of 5 when specified, and null in the second call, where the default isn't specified.

Community
  • 1
  • 1
nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • I didn't understood the definition from Internet. Now I am clarified. But I would like to know what is the use of Integer value of a System property – niks Feb 01 '16 at 19:03
  • 2
    @niks it looks like you've answered that question for yourself: it's to get properties like `sun.arch.data.model`, which tells you if you are running in a 32-bit or 64-bit JVM. – Louis Wasserman Feb 01 '16 at 19:06
  • 1
    @niks As an additional example, a system property stating a memory limit or timeout would also be numeric. – nanofarad Feb 01 '16 at 19:09