The four ways to turn a string into an integer

Java gives you four main paths to convert a string to an integer: Integer.parseInt(), Integer.valueOf(), Integer.decode(), and Integer.getInteger(). The first two handle nearly all real situations. Integer.parseInt() returns a primitive int, while Integer.valueOf() returns an Integer object. Both throw a NumberFormatException if the string contains non-numeric characters or is null.

For most code you write, Integer.parseInt() is the right choice because it is direct and fast. It takes a string like "42" and produces the number 42. If you need an Integer object instead of a primitive int — because you are putting it in a collection or passing it to a method that expects an object — use Integer.valueOf(). The other two methods exist for specific cases: Integer.decode() handles strings with prefixes like "0x" for hexadecimal, and Integer.getInteger() reads system properties, which almost no one uses.

Key Takeaways

  • Integer.parseInt("42") converts a string to a primitive int and is the fastest, most common choice for conversion.
  • Integer.valueOf("42") converts a string to an Integer object, which you need when storing values in collections or passing to methods that expect objects.
  • Both methods throw NumberFormatException if the string contains letters, spaces, or special characters, so wrap them in try-catch if the input is untrusted.
  • Integer.decode() handles strings with hex or octal prefixes like "0xFF", while Integer.getInteger() reads system properties and is rarely used in normal code.

Using Integer.parseInt() for simple conversion

Integer.parseInt() is the standard tool for converting a string to a primitive int. Call it with the string as the argument: int number = Integer.parseInt("42"); The result is a primitive int value of 42. This method is fast and produces no wrapper object, so it uses less memory than Integer.valueOf().

The method accepts an optional second argument for the base (radix). To convert a binary string, use Integer.parseInt("1010", 2); which produces 10. For hexadecimal, use base 16: Integer.parseInt("FF", 16); produces 255. The base can be anything from 2 to 36, though 2, 8, 10, and 16 are the ones you will actually use.

If the string cannot be converted — because it contains letters, spaces, or other non-numeric characters — the method throws NumberFormatException. A string like "42abc" or "4 2" will fail. If you are reading input from a user or a file, wrap the call in a try-catch block to handle bad input gracefully.

Using Integer.valueOf() when you need an object

Integer.valueOf() does the same conversion as Integer.parseInt() but returns an Integer object instead of a primitive int. Use it when you need to store the value in a collection like ArrayList or HashMap, or when a method expects an Integer rather than an int. The call looks the same: Integer number = Integer.valueOf("42");

One advantage of Integer.valueOf() is that it caches values between -128 and 127. If you convert the string "50" twice, both calls return the same cached object in memory. This saves memory in code that converts many small numbers, though the difference is tiny in most programs. For values outside that range, each call creates a new object.

Like Integer.parseInt(), Integer.valueOf() throws NumberFormatException if the string is invalid. It also accepts a radix argument: Integer.valueOf("FF", 16); produces an Integer object with the value 255. If you are unsure whether to use parseInt() or valueOf(), choose parseInt() unless the code explicitly needs an object.

Handling errors when conversion fails

Both parseInt() and valueOf() throw NumberFormatException when the string cannot be converted. This happens with strings like "abc", "12.34", "12 34", or null. You must catch this exception or your program will crash. The simplest pattern is a try-catch block around the conversion:

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

If you want to provide a default value when conversion fails, use a conditional inside the catch block or wrap the whole thing in a helper method. Another approach is to check the string before converting — for example, using a regular expression to verify it contains only digits. This prevents the exception from being thrown in the first place, which is faster if you expect many invalid inputs.

Converting strings with plus or minus signs

Both parseInt() and valueOf() handle strings that start with a plus or minus sign. Integer.parseInt("-42") produces -42, and Integer.parseInt("+42") produces 42. Spaces before or after the number cause a NumberFormatException, so "42 " and " 42" will both fail. If you need to handle strings with leading or trailing spaces, call .trim() first: Integer.parseInt(userInput.trim())

When using a radix argument, the sign still works: Integer.parseInt("-FF", 16) produces -255. The sign must come before any prefix like "0x", so "-0xFF" works but "0x-FF" does not.

Integer.decode() for hex and octal strings

Integer.decode() converts strings that have prefixes indicating the number base. Use it when the string itself tells you whether it is hexadecimal, octal, or decimal. A string starting with "0x" or "0X" is treated as hexadecimal, "0" alone means octal, and anything else is decimal. Integer.decode("0xFF") produces 255, while Integer.decode("077") produces 63 (octal).

This method is useful when reading configuration files or user input that may contain numbers in different bases. It throws NumberFormatException just like parseInt() and valueOf(), so wrap it in try-catch if the input is untrusted. Most code does not need decode() because you usually know the base in advance and can use parseInt() with an explicit radix argument.

Why Integer.getInteger() is rarely used

Integer.getInteger() reads system properties, not strings. It takes a property name like "java.version" and returns the integer value if that property exists. This method is almost never used in normal application code because system properties are typically strings or booleans, not integers. If you find yourself thinking about getInteger(), you probably want parseInt() or valueOf() instead.

The method returns null if the property does not exist, so you must check for null before using the result. It also accepts a default value as a second argument: Integer.getInteger("myProperty", 42) returns 42 if the property is not found. This is useful only in rare cases where your application reads configuration from system properties set at startup.

Frequently Asked Questions

What is the difference between parseInt and valueOf?

Integer.parseInt() returns a primitive int, while Integer.valueOf() returns an Integer object. Use parseInt() for speed and simplicity. Use valueOf() when you need to store the value in a collection or pass it to a method that expects an Integer object. Both throw the same exception on invalid input.

Can I convert a string with a decimal point?

No, Integer.parseInt() and Integer.valueOf() reject strings like "42.5" and throw NumberFormatException. If you need to convert a decimal string, use Double.parseDouble() first, then cast to int: int number = (int) Double.parseDouble("42.5"); This truncates the decimal part.

What happens if I convert a very large number?

If the string represents a number larger than 2,147,483,647 (the maximum int value), parseInt() and valueOf() throw NumberFormatException. Use Long.parseLong() instead for larger numbers: long number = Long.parseLong("9999999999");

Do I need to trim whitespace before converting?

Yes, if the string has leading or trailing spaces. Integer.parseInt(" 42 ") throws NumberFormatException. Call .trim() first: Integer.parseInt(userInput.trim()); Spaces inside the number like "4 2" always cause an error and cannot be fixed by trimming.

Which method is fastest?

Integer.parseInt() is slightly faster than Integer.valueOf() because it does not create an object. The difference is negligible in most code — only optimize if profiling shows conversion is a bottleneck. Choose the method based on whether you need a primitive int or an Integer object, not on speed.