The three main ways to convert a string to an integer

Java gives you three straightforward methods to turn a string into an integer: Integer.parseInt(), Integer.valueOf(), and the Integer constructor. The first two are the ones you'll use almost all the time. Integer.parseInt() returns a plain integer value, while Integer.valueOf() returns an Integer object that wraps that value. For most situations, Integer.parseInt() is what you want because it's simpler and slightly faster.

The difference matters only if your code needs to store the result in a variable that expects an object rather than a primitive value. In practice, Java automatically converts between the two when needed, so you can usually pick whichever feels natural to you.

All three methods will throw an exception if the string doesn't contain a valid number — for example, if it has letters mixed in or is completely empty. You'll need to handle that error, which we'll cover in the section below.

Key Takeaways

  • Integer.parseInt("123") converts the string "123" into the integer 123 and is the most common choice for this task.
  • Integer.valueOf("123") does the same thing but returns an Integer object instead of a primitive int, which matters only in specific situations.
  • Both methods throw an exception if the string contains non-numeric characters, so you need to catch that error or validate the string first.
  • You can specify a number base (like binary or hexadecimal) as a second argument if the string represents something other than a decimal number.

Using Integer.parseInt() for basic conversion

Integer.parseInt() is the standard tool. You pass it a string, and it returns an integer. Here's the actual syntax:

int number = Integer.parseInt("456");

After this line runs, the variable number holds the integer 456. The string "456" is gone — it's been converted to a numeric value that Java can do math with. If you try to convert a string that isn't a valid number, like "456abc" or "hello", the program will crash with a NumberFormatException unless you catch it.

You can also convert strings that start with a plus or minus sign: Integer.parseInt("-789") works fine and gives you -789. Leading and trailing spaces are ignored, so Integer.parseInt(" 100 ") also works.

Handling errors when conversion fails

When Integer.parseInt() encounters a string it can't convert, it throws a NumberFormatException. Your program stops unless you catch that error. The standard way to do this is with a try-catch block:

try { int number = Integer.parseInt(userInput); } catch (NumberFormatException e) { System.out.println("That's not a valid number"); }

The code inside the try block runs normally. If an exception happens, the catch block runs instead, and you can print an error message, ask the user to try again, or set a default value. Without this try-catch, a bad input will crash your entire program.

A simpler approach for some situations is to check the string before you convert it. You can use a method like matches() to test whether the string contains only digits: if (userInput.matches("-?\\d+")) will return true only for valid integers (including negative ones). This lets you avoid the exception entirely.

Using Integer.valueOf() when you need an object

Integer.valueOf() does the same conversion as Integer.parseInt(), but it returns an Integer object instead of a primitive int. The difference is subtle but matters in specific cases:

Integer number = Integer.valueOf("789");

Now number is an Integer object, not a plain integer. This matters when you're working with collections like ArrayList or HashMap, which store objects. It also matters if a method expects an Integer object specifically. In most modern Java code, you won't notice the difference because Java automatically converts between int and Integer when needed — a feature called autoboxing.

Integer.valueOf() also caches values between -128 and 127, which means repeated conversions of the same small number are slightly faster. For large numbers or one-time conversions, this makes no practical difference.

Converting strings in different number bases

If your string represents a number in a base other than decimal (base 10), you can tell Integer.parseInt() which base to use. The second argument specifies the base:

Integer.parseInt("1010", 2) converts the binary string "1010" to the decimal integer 10. Integer.parseInt("FF", 16) converts the hexadecimal string "FF" to the decimal integer 255. Integer.parseInt("77", 8) converts the octal string "77" to the decimal integer 63.

The base can be anything from 2 to 36. This is useful when you're reading data from files or networks that use non-decimal formats, or when you're working with color codes (hexadecimal) or file permissions (octal).

Common mistakes and how to avoid them

The most frequent error is forgetting to handle the NumberFormatException. If you're reading input from a user or a file, assume it might not be a valid number and wrap your conversion in a try-catch block. The second common mistake is passing a string with spaces or special characters and expecting it to work — Integer.parseInt() ignores leading and trailing spaces but will fail on spaces in the middle or on any non-numeric characters.

Another mistake is confusing Integer.parseInt() with Integer.toString(), which does the opposite conversion (turning an integer into a string). If you need to go from int to String, use Integer.toString(123) instead.

Finally, remember that integers in Java have limits. The largest integer is 2,147,483,647 and the smallest is -2,147,483,648. If your string represents a number larger than that, Integer.parseInt() will fail. For very large numbers, use Long.parseLong() instead, which handles numbers up to 9,223,372,036,854,775,807.

Frequently Asked Questions

What's the difference between Integer.parseInt() and Integer.valueOf()?

Integer.parseInt() returns a primitive int, while Integer.valueOf() returns an Integer object. For most code, they work the same way because Java automatically converts between them. Use parseInt() if you're doing math or storing in a straightforward variable; use valueOf() if you're putting the result in a collection or passing it to a method that expects an object.

Why does my conversion throw a NumberFormatException?

The string contains something that isn't a valid number — letters, symbols, or spaces in the middle. Check what the string actually contains by printing it. Remember that Integer.parseInt() ignores leading and trailing spaces but fails on anything else that isn't a digit, a leading plus or minus sign, or part of a valid number format.

Can I convert a decimal number like "3.14" to an integer?

No, Integer.parseInt("3.14") will throw an exception because the decimal point isn't allowed. If you need to convert a decimal string, use Double.parseDouble() first, then cast it to int: int number = (int) Double.parseDouble("3.14"). This gives you 3, dropping the decimal part.

What happens if the string is too large for an integer?

Integer.parseInt() throws a NumberFormatException if the number is larger than 2,147,483,647 or smaller than -2,147,483,648. Use Long.parseLong() instead if you need to handle bigger numbers.

How do I convert a string with a plus sign like "+42"?

Integer.parseInt("+42") works fine and returns 42. The plus sign is treated the same way as no sign at all. Negative numbers with a minus sign also work: Integer.parseInt("-42") returns -42.