How to Change String to Int in Java: Methods, Tradeoffs, and When Each Applies

Converting a String to an integer is one of the most common operations in Java development — and one of the first places new developers run into runtime exceptions. Java gives you several ways to do this conversion, each with different behavior around errors, performance, and use case fit. Understanding the differences matters more than memorizing syntax.

Why String-to-Int Conversion Isn't Automatic in Java

Java is a statically typed language, which means the compiler enforces strict separation between data types. A String like "42" and an int like 42 are fundamentally different in memory — one is an object, one is a primitive. Java won't coerce between them silently the way some loosely typed languages do.

That design choice is intentional. It forces developers to handle the conversion explicitly, which means deciding what happens when the input isn't a valid number. That decision — what to do with bad data — is actually the most important part of string-to-int conversion.

The Main Methods for Converting String to Int in Java

Integer.parseInt()

This is the most widely used approach. It takes a String argument and returns a primitive int.

String value = "123"; int result = Integer.parseInt(value); 

If the string can't be parsed — because it contains letters, symbols, or is nullparseInt() throws a NumberFormatException. That exception must either be caught or allowed to propagate.

try { int result = Integer.parseInt("abc"); } catch (NumberFormatException e) { System.out.println("Invalid number format"); } 

parseInt() also accepts a radix (base) as a second argument, which is useful when working with binary, octal, or hexadecimal strings:

int binary = Integer.parseInt("1010", 2); // returns 10 int hex = Integer.parseInt("1F", 16); // returns 31 

Integer.valueOf()

This method returns an Integer object (the wrapper class) rather than a primitive int. In most cases, Java's autoboxing will convert it to a primitive automatically, so practical usage looks similar:

Integer result = Integer.valueOf("456"); 

The key distinction: Integer.valueOf() caches commonly used values (typically -128 to 127), which can offer minor memory efficiency in loops or high-frequency operations. For most applications, the difference is negligible.

Integer.decode()

Less commonly used, decode() can handle strings that represent numbers in hexadecimal (0x or # prefix) or octal (0 prefix) format automatically:

int hex = Integer.decode("0xFF"); // returns 255 int octal = Integer.decode("010"); // returns 8 

This is useful in configuration parsing or when reading values from files that might use different numeric notations.

Scanner or BufferedReader with Parsing

When reading user input or file data, conversion is often embedded directly in the read operation:

Scanner scanner = new Scanner(System.in); int value = scanner.nextInt(); 

nextInt() handles the conversion internally but still throws InputMismatchException if the input isn't a valid integer — so error handling still applies.

Comparing the Core Methods 🔍

MethodReturnsThrows on Bad InputHandles RadixBest For
Integer.parseInt()int (primitive)NumberFormatExceptionYes (optional)General use, performance-sensitive code
Integer.valueOf()Integer (object)NumberFormatExceptionYes (optional)When an object is needed; caching benefit
Integer.decode()Integer (object)NumberFormatExceptionAuto-detects prefixHex/octal string inputs
new Integer()Integer (object)NumberFormatExceptionNoDeprecated since Java 9

Handling null and Edge Cases

A frequent source of bugs: Integer.parseInt(null) throws a NumberFormatException, not a NullPointerException. That's easy to miss. If your string might be null, check before parsing:

if (value != null && !value.isEmpty()) { int result = Integer.parseInt(value); } 

Whitespace is another edge case. " 42 " with leading or trailing spaces will fail parsing. Use .trim() first if your input source isn't tightly controlled:

int result = Integer.parseInt(value.trim()); 

For very large numbers that exceed int range (above 2,147,483,647), parseInt() will throw an exception. In those cases, Long.parseLong() or BigInteger are the appropriate tools.

What Determines the Right Approach for Your Situation

Several factors shape which method makes sense:

  • Where the string comes from — user input, a database, a file, an API response, or hardcoded values each carry different reliability assumptions
  • How you handle failures — whether a bad value should crash the operation, use a default, or trigger a retry loop
  • Whether you need a primitive or an object — methods that take Integer objects (like those in collections) can't accept primitive int directly
  • Java versionnew Integer() constructor is deprecated in Java 9+ and removed later; parseInt() and valueOf() remain current
  • Number base — if your strings represent hex or binary values, the radix parameter or decode() changes the result significantly ⚠️

The Error Handling Decision Is Where It Gets Personal

Every conversion method in Java will fail on bad input — the only question is how your code responds. A command-line tool that accepts user input needs different error handling than an internal data pipeline processing validated records. A mobile app might show the user a message; a background service might log and skip; a financial system might halt and alert.

The mechanics of string-to-int conversion in Java are straightforward. The part that varies by developer, team, and application is what "good input" looks like in your specific context — and what the right failure behavior is when that assumption breaks. 🧩