10 Methods to Convert String to Int in Java

java convert string to int on macbook displayI think you will agree with me when I say:

Converting String to int in Java is a REALLY one of the most used operations.

I’ll show 10 methods how to change String to int with description and examples.

How to Convert a String to an Int in Java Methods Overview

  • If you need to parse String to primitive int – use Integer.parseInt
  • If you are sure that result is always positive and you should convert String to primitive int – use Integer.parseUnsignedInt
  • If you need to convert String to Integer object – use Integer.valueOf
  • If you have String in special format (“0x“, “0X“, “#“) – use Integer.decode
  • If you need to turn a string into an int and specify default value in case of NumberFormatException – use NumberUtils.toInt from apache.commons.lang

NOTE: It’s impossible to cast String object to primitive int or to Integer object. This code won’t compile. You should use methods described here to parse string to int.

Integer.parseInt(s)

Converts a String value as a signed primitive int.

Method signature:

Example:

Output:

Reference:

Tweet This MethodClick To Tweet

Integer.parseInt(s, radix)

Parses a String argument as a signed primitive int in the radix specified by the second argument.

Method signature:

Examples:

Output:

Since: Java 8

Reference:

Tweet This MethodClick To Tweet

Integer.parseInt(s, beginIndex, endIndex, radix)

Turning a CharSequence value into a signed primitive int in the radix specified by the second argument from beginIndex to endIndex.

Note: The method has no protection against CharSequence mutation during parsing.

Method signature:

Examples:

Output:

Since: Java 9

Reference:

Tweet This MethodClick To Tweet

Integer.parseUnsignedInt(s)

Converts a String value to an unsigned primitive int.

Method signature:

Examples:

Reference:

Tweet This MethodClick To Tweet

Integer.parseUnsignedInt(s, radix)

Parses a String argument as an unsigned primitive int in the radix specified by the second argument.

Method signature:

Examples:

Since: Java 8

Reference:

Tweet This MethodClick To Tweet

Integer.parseUnsignedInt(s, beginIndex, endIndex, radix)

Turning a CharSequence value into an unsigned primitive int in the radix specified by the second argument from beginIndex to endIndex.

Method signature:

Examples:

Output:

Since: Java 9

Reference:

Tweet This MethodClick To Tweet

Integer.valueOf(s)

Converts a String to an Integer object.

Method signature:

Example:

Output:

Reference:

Tweet This MethodClick To Tweet

Integer.valueOf(s, radix)

Parses a String as an Integer object in the radix specified by the second argument.

Method signature:

Examples:

Output:

Since: Java 8

Reference:

Tweet This MethodClick To Tweet

Integer.decode(s)

Decodes a String into an Integer. Accepts decimal, hexadecimal, and octal numbers in the following format:

Sign Opt Name Examples
 ±  DecimalNumeral  555, -475
 ±  0X  HexDigits  0XA, -0X12A
 ±  0x  HexDigits  0xA, -0x12A
 ±  #  HexDigits  -#A, #123
 ±  0  OctalDigits  010, -010

Method signature:

Examples:

Output:

Reference:

Tweet This MethodClick To Tweet

NumberUtils.toInt(s)

NumberUtils class contains some useful methods to parse strings. To Include it to the project you should add maven dependency:

and import org.apache.commons.lang3.math.NumberUtils to your class.

Method signature:

Example:

Output:

Implementation with a default value as a second argument would be useful if you’re not sure what’s inside of the input String and you want to provide a fallback value.

Method signature:

Example:

If the string is null, the default value is returned.

Output:

Reference:

Tweet This MethodClick To Tweet

Any questions? Please, ask me.

Scroll Up