Converting Strings to Numbers and Vice Versa

A young businessman is working at his notebook in his office.
Oleg Prikhodko/E+/Getty Images

Typically in a graphical user interface there will be text fields that are expecting the user to enter in a numerical value. This number value will end up in a String object which doesn't really help your program if you want to do some arithmetic. Fortunately there are wrapper classes that provide methods for converting those String values into numbers and the String class has a method to convert them back again.

Wrapper Classes

The primitive data types that deal with numbers (i.e, byte, int, double, float, long and short) all have class equivalents. These classes are known as wrapper classes as they take a primitive data type, and surround it with the functionality of a class. For example, the Double class will have a double value as it's data and provide methods for manipulating that value.

All of these wrapper classes have a method called valueOf. This method take a String as an argument and returns an instance of the wrapper class. For example, let's say we have a String with the value of ten:

String number = "10";

Having this number as a String is no use to us so we use the Integer class to convert it into an Integer object:

Integer convertedNumber = Integer.valueOf(number);

Now the number can be used as a number and not a String:

convertedNumber = convertedNumber + 20;

You can also make the conversion go straight to a primitive data type:

int convertedNumber = Integer.valueOf(number).intValue();

For other primitive data types you just slot in the correct wrapper class - Byte, Integer, Double, Float, Long, Short.

Note: You must make sure the String can be parsed into the appropriate data type. If it can't you will end up with a runtime error.

For example, trying to covert "ten" into an integer:

String number = "ten";
int convertedNumber = Integer.valueOf(number).intValue();

will produce a NumberFormatException because the compiler has no idea "ten" is supposed to be 10.

More subtly the same error will occur if you forget that an int can only hold whole numbers:

String number = "10.5";  
int convertedNumber = Integer.valueOf(number).intValue();

The compiler won't truncate the number it will just think that it doesn't fit into an int and that it's time to throw a NumberFormatException.

Converting Numbers to Strings

To make a number into a String follows the same sort of pattern as the String class has a valueOf method too. It can take any of the primitive data type numbers as an argument and produce a String:

int numberTwenty = 20;
String converted = String.valueOf(numberTwenty);

which puts "20" as the String value of converted.

or you can use the toString method of any of the wrapper classes:

String converted = Integer.toString(numberTwenty);

The toString method is common to all object types - most of the time it is just a description of the object. For wrapper classes this description is the actual value they contain. In this direction the conversion is a bit more robust.

If I was to use the Double class instead of the Integer:

String converted = Double.toString(numberTwenty);

the result would not cause a runtime error. The converted variable would contain the String "20.0".

There is also a more subtle way to convert numbers when you are concatenating Strings.  If I was to build a String like:

String aboutDog = "My dog is " + numberTwenty + " years old.";

the conversion of the int numberTwenty is automatically done.

Example Java code can be found in the Fun With Strings Example Code.