Integer class has three methods called as getInteger, parseInt and valueOf. Each of these methods solve a specific purpose. In this post, we’ll discuss the differences between them with examples.
On a high-level below are the differences between these 3 methods.
getInteger | parseInt | valueOf |
---|---|---|
It determines the integer value of a given system property | It parses a String object containing numbers as an int type | It converts a given value to a wrapper class Integer type |
First version takes String as an argument which is a system property and returns its value as Integer. It throws SecurityException | First version takes number in the form of String object and return it as int value. It throws NumberFormatException | First version takes a primitive type int value, returns Integer and doesn’t throw any exception |
Second version takes String and default value as int and returns Integer. It also throws SecurityException | Second version takes a String and numeric system and returns int. It throws NumberFormatException | Second version takes a String object as an argument, returns Integer and throws NumberFormatException |
NA | Third version reads part of the String and converts it into int based on a given number system. It throws NumberFormatException, NullPointerException and IndexOutOfBoundsException | Third version takes a String object and converts it into Integer based on given number system and throws NumberFormatException |
Returns an Integer object | Returns a primitive type int value | Returns an Integer object |
Let us look into the differences in detail.
Integer.getInteger
Signature of Integer.getInteger Method:
Integer getInteger(String) throws SecurityException
Integer getInteger(String, int) throws SecurityException
The first version of Integer.getInteger takes a system property key as an argument and returns the value of that system property as an Integer object. If the property is not found it returns null. However, the second version of Integer.getInteger takes a second argument of type int which is a default value which will be returned if the system property is not found.
Integer.getInteger uses the argument passed to it and calls System.getProperty method to get the system property value and then decode it as Integer object before returning it to the calling function. If the property is not found
Integer.getInteger also propagates the SecurityException if a security manager doesn’t allow access to the given system property.
Example:
package com.techstackjournal;
public class Main {
public static void main(String[] args) {
System.setProperty("prop", "100");
System.out.println(Integer.getInteger("prop"));
System.out.println(Integer.getInteger("unknownprop",0));
}
}
Output:
100
0
Integer.parseInt
Signature of Integer.parseInt method:
int parseInt(String str) throws NumberFormatException
int parseInt(String str, int radix) throws NumberFormatException
int parseInt(CharSequence str, int beginIndex, int endIndex, int radix) throws NullPointerExcepion, IndexOutOfBoundsException, NumberFormatException
The first version of Integer.parseInt method takes number as a String object and parses and return it as an int primitive type value.
Second version of Integer.parseInt method apart from the String object takes an int argument called radix, which tells what number system it will use to determine the number. If the radix is 2, it will identify the number in String as binary, if it is 8 it will be considered as octal so on and so forth.
The third version of Integer.parseInt can select part of the String and returns an int based on the given number system. This method takes CharSequence which is a super class of String as the first argument, second argument tells the starting character from where number to be extracted, third argument tells till what character to be stripped and fourth argument tells the number system of the selected number.
package com.techstackjournal;
public class Main {
public static void main(String[] args) {
System.out.println(Integer.parseInt("100"));
System.out.println(Integer.parseInt("100", 2));
System.out.println(Integer.parseInt("$1000",1,5,10));
}
}
Output:
100
4
1000
Integer.valueOf
Signature of Integer.valueOf method:
Integer valueOf(int)
Integer valueOf(String) throws NumberFormatException
Integer valueOf(String str, int radix) throws NumberFormatException
First version of Integer.valueOf method takes an int type value and converts it into an Integer object. This method doesn’t throw any exception.
Second version of Integer.valueOf method takes String object similar to parseInt, however it returns Integer object unlike parseInt which returns int. It may throw NumberFormatException if the given String is not valid number.
Third version of Integer.valueOf method takes String object and a number system value as radix. The valueOf method will treat the give number in the given number system and returns it as Integer object.
Example:
package com.techstackjournal;
public class Main {
public static void main(String[] args) {
System.out.println(Integer.valueOf(10));
System.out.println(Integer.valueOf("10"));
System.out.println(Integer.valueOf("100", 2));
}
}
Output:
10
10
4