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 Integer.decode(). The first two are the ones you'll use almost every 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 faster.

The key difference between these methods matters when your string contains invalid characters or isn't actually a number. All three will throw an exception — a Java error that stops your program — if the string can't be converted. You need to handle that error, which we'll cover in the next section.

Key Takeaways

  • Integer.parseInt() is the standard method: it takes a string and returns an integer, and it's the fastest option for most code.
  • Your string must contain only digits, optionally with a plus or minus sign at the start, or Java will throw a NumberFormatException error.
  • Wrap your conversion in a try-catch block to handle strings that aren't valid numbers without crashing your program.
  • Integer.valueOf() does the same job as parseInt() but returns an Integer object instead of a plain int, which matters only in specific situations.

Using Integer.parseInt() for the most common case

Integer.parseInt() is the method you'll reach for in almost every situation. Write it like this:

String myString = "42"; int myNumber = Integer.parseInt(myString);

The first line creates a string that contains the text "42". The second line converts that string to an actual integer and stores it in the variable myNumber. Now you can use myNumber in math, comparisons, or any other operation that needs a number instead of text.

The string can also start with a plus or minus sign: Integer.parseInt("+42") and Integer.parseInt("-42") both work fine. But if your string contains spaces, letters, or special characters — like "42 dollars" or "4.2" — Java will throw a NumberFormatException and your program will crash at that line unless you catch the error.

Handling strings that aren't valid numbers

When you convert a string to an integer, you're taking a risk: the string might not actually be a number. If you try to convert "hello" or "3.14" or "42 cents", Java throws a NumberFormatException. Your program stops running unless you tell it what to do when that happens.

Use a try-catch block to handle the error safely:

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

The try block contains the code that might fail. If Integer.parseInt() throws an exception, Java jumps to the catch block instead of crashing. Inside the catch block, you decide what happens next — print a message, use a default value, ask the user to try again, or log the error. The program keeps running instead of stopping.

When to use Integer.valueOf() instead

Integer.valueOf() does almost the same job as Integer.parseInt(), but it returns an Integer object instead of a plain int. For most everyday code, this difference doesn't matter. But in specific situations — when you're storing values in a collection, working with generics, or need the object wrapper for other reasons — Integer.valueOf() is the right choice.

String myString = "42"; Integer myNumber = Integer.valueOf(myString);

Notice the capital I in Integer on the second line. That's the object type, not the primitive int type. Integer.valueOf() also throws a NumberFormatException if the string isn't a valid number, so you still need a try-catch block around it. If you're not sure which one to use, start with Integer.parseInt() — it's simpler and faster for the majority of cases.

Converting strings with different number formats

By default, Integer.parseInt() assumes your string is a base-10 (decimal) number. If you need to convert a string that represents a number in a different base — binary, hexadecimal, or octal — you can tell parseInt() which base to use:

String hexString = "2A"; int myNumber = Integer.parseInt(hexString, 16);

The second argument (16) tells parseInt() to treat the string as hexadecimal. For binary, use 2; for octal, use 8. The string must contain only valid digits for that base. "2A" is valid hexadecimal (A represents 10), but "2G" would throw an exception because G doesn't exist in hexadecimal.

Integer.decode() handles this automatically for you if the string starts with a prefix: "0x" for hexadecimal, "0" for octal, or no prefix for decimal. Integer.decode("0x2A") returns 42, the same as Integer.parseInt("2A", 16). Use decode() when you're reading strings that already have these prefixes built in.

Common mistakes and how to avoid them

The most common mistake is forgetting that Integer.parseInt() throws an exception when the string isn't a valid number. You write the code, it works fine during testing with good data, then crashes in production when a user enters something unexpected. Always wrap your conversion in a try-catch block, or validate the string before you convert it.

Another frequent error is including spaces or decimal points in the string. "42" converts fine, but " 42" (with a leading space) throws an exception. "42.5" throws an exception because parseInt() doesn't handle decimals — use Double.parseDouble() for that instead. If you need to strip spaces first, call myString.trim() before parsing.

A third mistake is confusing Integer.parseInt() with Integer.valueOf(). They do almost the same thing, but parseInt() returns an int and valueOf() returns an Integer object. If you assign the result to an int variable, Java will automatically unwrap the Integer object, so the difference usually doesn't matter. But understanding which is which helps you read other people's code and choose the right one for your situation.

Frequently Asked Questions

What's the difference between int and Integer in Java?

int is a primitive type — a basic building block of the language that holds a number directly. Integer is an object that wraps an int value inside it. For most everyday code, int is simpler and faster. Integer is useful when you need to store values in collections or pass them to methods that expect objects instead of primitives.

Can I convert a string with a decimal point like "3.14"?

No, Integer.parseInt() will throw a NumberFormatException because it only handles whole numbers. If you need to convert "3.14", use Double.parseDouble() instead, which returns a decimal number. If you need to keep it as an integer, you'll have to remove the decimal part yourself first.

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

Java integers have a maximum value of 2,147,483,647. If your string represents a larger number, Integer.parseInt() throws a NumberFormatException. Use Long.parseLong() instead if you need to handle bigger numbers.

Do I always need a try-catch block?

Technically no — your code will compile without one. But your program will crash if the string isn't a valid number and you haven't caught the exception. In production code, always use a try-catch block or validate the string first to prevent unexpected crashes.

Is Integer.parseInt() faster than Integer.valueOf()?

Yes, slightly. Integer.parseInt() returns a plain int directly, while Integer.valueOf() creates an Integer object, which takes a tiny bit more time and memory. For most applications the difference is invisible, but in performance-critical code where you're converting millions of strings, parseInt() is the better choice.