 ## 1. Overview

As we know, converting a numeric String to an int or Integer is a very common operation in Java.

In this tutorial, we'll go through two very popular static methods, parseInt() and valueOf() of the  java.lang.Integer class which help us do this conversion. Moreover, we'll also understand a few differences between these two methods using simple examples.

## 2. The parseInt() Method

The class java.lang.Integer provides three variants of the parseInt() method. Let's look at each of them.

### 2.1. Convert String to Integer

The first variant of parseInt() accepts a String as a parameter and returns the primitive data type int. It throws NumberFormatException when it cannot convert the String to an integer.

Let's look at its signature:

``public static int parseInt(String s) throws NumberFormatException``

Now, we'll see a few examples where we pass signed/unsigned numeric strings as parameters to it to understand how parsing from string to integer happens:

``````@Test
public void whenValidNumericStringIsPassed_thenShouldConvertToPrimitiveInt() {
assertEquals(11, Integer.parseInt("11"));
assertEquals(11, Integer.parseInt("+11"));
assertEquals(-11, Integer.parseInt("-11"));
}``````

### 2.2. Specifying Radix

The second variant of the parseInt() method accepts a String and an int as parameters and returns the primitive data type int. Just like the first variant we saw, it also throws NumberFormatException when it cannot convert the String to an integer:

``public static int parseInt(String s, int radix) throws NumberFormatException``

By default, the parseInt() method assumes that the given String is a base-10 integer. Here, the parameter radix is the radix or base to be used for string to integer conversion.

To understand this better, let's look at a few examples where we pass a string along with the radix parameter to parseInt():

``````@Test
public void whenValidNumericStringWithRadixIsPassed_thenShouldConvertToPrimitiveInt() {
assertEquals(17, Integer.parseInt("11", 16));
assertEquals(10, Integer.parseInt("A", 16));
assertEquals(7, Integer.parseInt("7", 8));
}``````

Now, let's understand how string conversion with radix takes place. For instance, in a number system with radix 13, a string of digits such as 398 denotes the decimal number (with radix/base 10) 632. In other words, in this case, here's how the calculation happens – 3 × 132 + 9 × 131 + 8 × 130 = 632.

Similarly, in the above example Integer.parseInt(“11”, 16) returned 17 with calculation, 1 × 161 + 1 × 160 = 17.

### 2.3. Convert Substring to Integer

Lastly, the third variant of the parseInt() method accepts a CharSequence, two integers beginIndex and endIndex of the substring, and another integer radix as parameters. If any invalid string is passed, it throws NumberFormatException:

``public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix) throws NumberFormatException``

JDK 9 introduced this static method in the Integer class. Now, let's see it in action:

``````@Test
public void whenValidNumericStringWithRadixAndSubstringIsPassed_thenShouldConvertToPrimitiveInt() {
assertEquals(5, Integer.parseInt("100101", 3, 6, 2));
assertEquals(101, Integer.parseInt("100101", 3, 6, 10));
}``````

Let's understand how substring conversion to integer with a given radix takes place. Here, string is “100101”, beginIndex and endIndex are 3 and 6, respectively. Hence, the substring is “101”. For expectedNumber1, radix passed is 2, which means it's binary. Therefore, substring “101” is converted to integer 5. Further, for expectedNumber2, the radix passed is 10, which means it is decimal. Consequently, substring “101” is converted to integer 101.

Additionally, we can see that Integer.parseInt() throws NumberFormatException when any invalid string is passed:

``````@Test(expected = NumberFormatException.class)
public void whenInValidNumericStringIsPassed_thenShouldThrowNumberFormatException(){
int number = Integer.parseInt("abcd");
}``````

## 3. The valueOf() Method

Next, let's take a look at the three variants of the valueOf() method provided by the class java.lang.Integer.

### 3.1. Convert String to Integer

The first variant of the valueOf() method accepts a String as a parameter and returns the wrapper class Integer. If any non-numeric string is passed, it throws NumberFormatException:

``public static Integer valueOf(String s) throws NumberFormatException``

Interestingly, it uses parseInt(String s, int radix) in its implementation.

Next, let's see a few examples of conversion from signed/unsigned numeric string to integer:

``````@Test
public void whenValidNumericStringIsPassed_thenShouldConvertToInteger() {
Integer expectedNumber = 11;
Integer expectedNegativeNumber = -11;

assertEquals(expectedNumber, Integer.valueOf("11"));
assertEquals(expectedNumber, Integer.valueOf("+11"));
assertEquals(expectedNegativeNumber, Integer.valueOf("-11"));
}``````

### 3.2. Convert int to Integer

The second variant of valueOf() accepts an int as a parameter and returns the wrapper class Integer. Also, it generates a compile-time error if any other data type such as float is passed to it.

Here's its signature:

``public static Integer valueOf(int i)``

In addition to int to Integer conversion, this method can also accept a char as a parameter and returns its Unicode value.

To understand this further, let's see a few examples:

``````@Test
public void whenNumberIsPassed_thenShouldConvertToInteger() {
Integer expectedNumber = 11;
Integer expectedNegativeNumber = -11;
Integer expectedUnicodeValue = 65;

assertEquals(expectedNumber, Integer.valueOf(11));
assertEquals(expectedNumber, Integer.valueOf(+11));
assertEquals(expectedNegativeNumber, Integer.valueOf(-11));
assertEquals(expectedUnicodeValue, Integer.valueOf('A'));
}``````

### 3.3. Specifying Radix

The third variant of valueOf() accepts a String and an int as parameters and returns the wrapper class Integer. Also, like all the other variants we've seen, it also throws NumberFormatException when it cannot convert the given string to Integer type:

``public static Integer valueOf(String s, int radix) throws NumberFormatException``

This method also uses parseInt(String s, int radix) in its implementation.

By default, the valueOf() method assumes that the given String represents a base-10 integer. Additionally, this method accepts another argument to change the default radix.

Let's parse a few String objects:

``````@Test
public void whenValidNumericStringWithRadixIsPassed_thenShouldConvertToInetger() {
Integer expectedNumber1 = 17;
Integer expectedNumber2 = 10;
Integer expectedNumber3 = 7;

assertEquals(expectedNumber1, Integer.valueOf("11", 16));
assertEquals(expectedNumber2, Integer.valueOf("A", 16));
assertEquals(expectedNumber3, Integer.valueOf("7", 8));
}``````

## 4. Differences Between parseInt() and valueOf()

To sum up, here are the main differences between the valueOf() and parseInt() methods:

Integer.valueOf() Integer.parseInt()
It returns an Integer object. It returns a primitive int.
This method accepts String and int as parameters. This method accepts only String as the parameter.
It uses Integer.parseInt() in its method implementation. It doesn't use any helper method to parse the string as an integer.
This method accepts a character as a parameter and returns its Unicode value. This method will produce an incompatible types error on passing a character as a parameter.

## 5. Conclusion

In this article, we learnt about the different implementations of parseInt() and valueOf() methods of the java.lang.Integer class. We also looked at the differences between the two methods.

As always, the complete code samples for this article can be found over on GitHub.

### Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE