JavaDevNotes.com

Java String To Integer Examples

This tutorial will show example codes on how to convert Java String To Int. Performing conversion from String to int is a common scenario when programming with Core Java.
Here are some quick reference example codes:

Convert using Integer.parseInt()

The Integer.parseInt() static method parses the string argument as a signed decimal integer and returns an int value.

Syntax

public static int parseInt(String s) throws NumberFormatException

The parameter s will be converted to a primitive int value. Note that the method will throw a NumberFormatException if the parameter is not a valid int.

Example
String numberAsString = "1234";
int number = Integer.parseInt(numberAsString);
System.out.println("The number is: " + number);
When you run the code above, the output will show this:
The number is: 1234

This is the most common and popular solution when you need to convert a String to int. Note that the resulting value is not an instance of the Integer class but just a plain primitive int value.


Convert using Integer.valueOf()

The Integer.valueOf() static method will return an Integer object holding the value of the specified String.

Syntax
public static Integer valueOf(String s) throws NumberFormatException

Note that the method will throw a NumberFormatException if the parameter is not a valid integer.

String numberAsString = "1234";
int number = Integer.valueOf(numberAsString);
System.out.println("The number is: " + number);
This will output:
The number is: 1234
This is the most common method when you wish to convert a String to Integer. Note that the resulting value is an instance of the Integer class and not a primitive int value.


Convert using new Integer(String).intValue()

Another alternative method is to create an instance of Integer class and then invoke it's intValue() method.

Example

String numberAsString = "1234";
Integer intObject = new Integer(numberAsString);
int number = intObject.intValue();

We can shorten to:

String numberAsString = "1234";
int number = new Integer(numberAsString).intValue();

or just:
int number = new Integer("1234").intValue();


Convert using DecimalFormat

The class java.text.DecimalFormat is a class that can be used to convert a number to it's String representation. It can also be used the other way around - it can parse a String into it's numerical representation.
Example
String numberAsString = "1234";
DecimalFormat decimalFormat = new DecimalFormat("#");
try {
   int number = decimalFormat.parse(numberAsString).intValue();
   System.out.println("The number is: " + number);
} catch (ParseException e) {
   System.out.println(numberAsString + " is not a valid number.");
}
Will output
The number is: 1234
Note that the parse() method will throw a ParseException when there are problems encountered with the String.


Convert with special radix

All of the examples above uses the base (radix) 10. There are cases when we wish to convert a Java String to Integer but using another base. Both Integer.parseInt() and Integer.valueOf() can receive a custom radix to be used in the conversion

Examples

  • Binary
    String numberAsString = "11111111";
    int number1 = Integer.valueOf(numberAsString, 2);
    int number2 = Integer.parseInt(numberAsString, 2);
    System.out.println(number1);
    System.out.println(number2);
    
    Will output:
    255
    255
    
  • Octal
    String numberAsString = "377";
    int number1 = Integer.valueOf(numberAsString, 8);
    int number2 = Integer.parseInt(numberAsString, 8);
    System.out.println(number1);
    System.out.println(number2);
    
    Will output:
    255
    255
    
  • Hexadecimal
    String numberAsString = "ff";
    int number1 = Integer.valueOf(numberAsString, 16);
    int number2 = Integer.parseInt(numberAsString, 16);
    System.out.println(number1);
    System.out.println(number2);
    
    Will output:
    255
    255
    

String Tutorials And Examples