Java is a versatile language that can be used for a variety of purposes. Converting a string to an integer is a common task and Java provides a number of methods for this purpose.

Using the Integer.parseInt() method

The Integer.parseInt() method in Java takes in a string and returns an int representing the number in the string. An example is shown below:

String str1 = "742";
int num1 = Integer.parseInt(str1);
System.out.println(num1); // prints 742

String str2 = "-14";
int num2 = Integer.parseInt(str2);
System.out.println(num2); // prints -14

If the string contains letters, has invalid symbols, or is simply too large to fit inside the range of an int (-2147483648 to 2147483647), the method throws an exception of the type NumberFormatException. You can handle this exception using try/catch and show an error message, or perform any other suitable action. An example is shown below:

// as this string has a letter, it will print 742a cannot be converted
String str1 = "742a";
try {
    int num1 = Integer.parseInt(str1);
    System.out.println(num1);
} catch (NumberFormatException e) {
    System.out.println(str1 + " cannot be converted");
}

// as this string is too long, it will print 2222222222 cannot be converted
String str2 = "2222222222";
try {
    int num2 = Integer.parseInt(str2);
    System.out.println(num2);
} catch (NumberFormatException e) {
    System.out.println(str2 + " cannot be converted");
}

Finally, you can also provide an optional radix to the Integer.parseInt() method. This is useful when you want to convert a string containing a number in a different base (such as binary, octal or hexadecimal), and it can be used like so:

String str1 = "1010";
int num1 = Integer.parseInt(str1, 2);
System.out.println(num1); // prints 10

String str2 = "ad";
int num2 = Integer.parseInt(str2, 16);
System.out.println(num2); // prints 173
  • In the first case, we're converting str1 to an int in base-2, aka, the binary number system. The number 1010 in binary can be converted to decimal as 1 × 23 + 0 × 22 + 1 × 21 + 0 × 20 = 10, and we get the same as the output.
  • In the second case, we're converting str2 to an int in base-16, aka, the hexadecimal number system. The number ad in hexadecimal can be converted to decimal as a (10) × 161 + d (13) × 160 = 173, and we get this value as the output.

The radix must be between 2 and 36. Similar to how hexadecimal uses the numbers 0-9 and the letters a-f to represent a hexadecimal digit, base-36 uses the numbers 0-9 and the letters a-z to represent numbers.

Using the Integer.valueOf() method

The Integer.valueOf() method is similar to Integer.parseInt() method, except it returns an Integer object rather than the primitive int type.

In the program below, we convert a string to an Integer using Integer.valueOf() and print the class of the object:

String str1 = "742";
Integer num1 = Integer.valueOf(str1);

// prints num1 = 742 and it is of class java.lang.Integer
System.out.println("num1 = " + num1 + " and it is of " + num1.getClass());

On the other hand, Integer.parseInt() returns the primitive type int, and we can confirm this by calling .getClass() on the int variable — it will result in a compile-time error.

String str1 = "742";
int num1 = Integer.parseInt(str1);

// this statement is WRONG as we cannot get the class of a primitive type
System.out.println("num1 = " + num1 + " and it is of " + num1.getClass());

Just as with Integer.parseInt(), it will throw a NumberFormatException in case the number can't be converted:

// 742a is not a valid number, so it prints 742a cannot be converted
String str1 = "742a";
try {
    Integer num1 = Integer.valueOf(str1);
} catch (NumberFormatException e) {
    System.out.println(str1 + " cannot be converted");
}

We can also provide an optional radix for base conversions:

String str2 = "1010";
Integer num2 = Integer.valueOf(str2, 2);
// 1010 in binary is 10 in decimal, so it prints 10
System.out.println(num2);

To make programming easier, Java 8 and above transparently convert primitive types to their corresponding classes (such as int to Integer), and the other way round through autoboxing and auto-unboxing. This means, you can technically write statements like these:

Integer num1 = Integer.parseInt("742");
int num2 = Integer.valueOf("349");

Even though Integer.parseInt() returns a int, it can be assigned to a Integer variable, and in a similar manner, even though Integer.valueOf() returns a Integer, it can be assigned to an int.

However, autoboxing and auto-unboxing results in performance overhead, so it is preferable to rely on Integer.parseInt() to convert strings to the int type, and Integer.valueOf() to convert strings to Integer type.