Integer.valueOf() vs Integer.parseInt() with Examples


In Java, both Integer.parseInt() and Integer.valueOf() methods are used for the conversion of a String into an Integer. These static methods belong to the Integer class of java.lang package and throws a NumberFormatException if the string is not a valid representation of an integer. But wait, the question arise here is that why Java provides two methods with the same functionality. Although they are used to serve similar jobs, there exist a few distinctions between them in terms of syntax and return type. In this article, we are going to explain the difference between Integer.parseInt() and Integer.valueOf() methods with the help of example programs.

Integer.parseInt() vs Integer.valueOf() in Java

Integer.parseInt() method in Java

The Integer.parseInt() method takes a string as a parameter and parses it into a primitive integer value. If we want to convert the specified String into other number systems like binary and hexadecimal, we can do this by providing a second parameter, which is the radix or base of the number system. For instance, if we want to parse a hexadecimal string, we can pass 16 as the radix.

Syntax

Integer.parseInt(string val1, int val2);

Here, the integer parameter 'val2' is optional known as radix.

Example 1

The following example illustrates the use of Integer.parseInt() method.

public class Example1 {
   public static void main(String[] args) {
      String stVal = "42";
      // converting string into integer
      int n1 = Integer.parseInt(stVal); 
      int n2 = Integer.parseInt(stVal, 16); // hexadecimal value
      // printing the result
      System.out.println("n1 = " + n1 + " , n2 = " + n2);
   }
}

Output

n1 = 42 , n2 = 66

Integer.valueOf() method in Java

The Integer.valueOf() method can take a string, a char or an int as a parameter but returns an Integer object that holds the value of the parsed integer. It can also convert the specified String into other number systems by passing the radix or base of the required number system as a parameter.

Syntax

Integer.valueOf(val1, val2);

Here, the integer parameter 'val2' is optional known as radix.

Example 2

The following example demonstrates the use of Integer.valueOf() method.

public class Example2 {
   public static void main(String[] args) {
      String stVal = "42";
      // converting string into integer
      int n1 = Integer.valueOf(stVal); 
      int n2 = Integer.valueOf(stVal, 16); // hexadecimal value
      // printing the result
      System.out.println("n1 = " + n1 + " , n2 = " + n2);
   }
}

Output

n1 = 42 , n2 = 66

Difference between Integer.valueOf() and Integer.parseInt() methods

The following table contains the distinction between Integer.valueOf() and Integer.parseInt() methods:

Integer.valueOf() Integer.parseInt()
Its return type is an Integer Wrapper Object. Its return type is a primitive integer value.
It can accept a string, a char or an int as a parameter. It accepts only a String type object.
When we pass a character as a parameter, it will return the corresponding ASCII value. When we pass a character as a parameter, the compiler will throw an error.
Integer.valueOf() can cache some values for better performance means calling this method with the same value may return the same object reference. Calling Integer.parseInt() method will always create a new integer value.

Now, let's discuss a few more examples to understand the difference between these methods more clearly.

Example 3

As mentioned earlier, if we pass a wrong String representation of an integer, both methods will throw a NumberFormatException. Let's have a look.

public class Example3 {
   public static void main(String[] args) {
      String s = "42.03";
      // converting string into integer
      int n1 = Integer.parseInt(s); 
      int n2 = Integer.valueOf(s); 
      // printing the result
      System.out.println("n1 = " + n1 + " , n2 = " + n2);
   }
}

Output

Exception in thread "main" java.lang.NumberFormatException: For input string: "42.03"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
	at java.base/java.lang.Integer.parseInt(Integer.java:665)
	at java.base/java.lang.Integer.parseInt(Integer.java:781)
	at Example3.main(Example3.java:5)

Example 4

If we pass a character as a parameter to Integer.parseInt() method, we will encounter “incompatible types: char cannot be converted to String” error as illustrated in the below example.

public class Example4 {
   public static void main(String[] args) {
      char chs = 't';
      // converting character into integer
      int n1 = Integer.parseInt(chs); 
      // printing the result
      System.out.println("n1 = " + n1);
   }
}

Output

Example4.java:5: error: incompatible types: char cannot be converted to String
        int n1 = Integer.parseInt(chs);

Example 5

If we pass a character as a parameter to Integer.valueOf() method, the code will work fine and return the corresponding ASCII value. We can also pass an integer type value.

public class Example5 {
   public static void main(String[] args) {
      int val1 = 56;
      char chs = 't';
      // converting character into integer
      int n1 = Integer.valueOf(chs); 
      // converting the integer value
      int n2 = Integer.valueOf(val1); 
      // printing the result
      System.out.println("n1 = " + n1 + " , n2 = " + n2);
   }
}

Output

n1 = 116 , n2 = 56

Conclusion

Both Integer.parseInt() and Integer.valueOf() are very useful methods for converting strings to integers. Generally, if we need a primitive int value, we can use Integer.parseInt(), and if we need an Integer wrapper object, we can use Integer.valueOf(). The performance of Integer.valueOf() method is significantly faster than Integer.parseInt() method.

Updated on: 20-Jul-2023

291 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements