Menu
CoddyTech

Type Casting Part 2

Part of the Fundamentals section of Coddy's Java journey. Lesson 14 of 73.

It is also possible to convert number and booleans to string and vice versa. To convert a value to string we can use the String.valueOf() function:

int number1 = 789;
double number2 = 789;
boolean isValid = true;
String text1 = String.valueOf(number1); // becomes "789"
String text2 = String.valueOf(number2); // becomes "789.0"
String text3 = String.valueOf(isValid); // becomes "true"

To convert a string to a different type is a bit more complicated:

String to Integer:

String numberText = "123";
int number = Integer.parseInt(numberText);    // becomes 123

String to Double:

String decimalText = "45.67";
double decimal = Double.parseDouble(decimalText);    // becomes 45.67

String to Boolean:

String boolText = "true";
boolean bool = Boolean.parseBoolean(boolText); // becomes true

parseBoolean will convert any case-insensitive string that has the value “true”. For example True, tRue, TRUE will all become true. Any other string, for example "yes" or "1", becomes false (no exception is thrown).

Trying to convert a string to an invalid type will result in an error:

String invalidNumber = "abc";
int number = Integer.parseInt(invalidNumber); // This will cause a NumberFormatException

A decimal string such as "12.5" is also invalid for Integer.parseInt() and throws a NumberFormatException: use Double.parseDouble() for decimals.

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Online Java compiler