Converting to and from Strings in Java
String.valueOf, toString, Integer.parseInt, NumberFormatException.
"1" + 2 + 3 is "123". 1 + 2 + "3" is "33". Same pieces, different answer. Java isn't random — it reads strictly left to right.Values into text
Three ways to turn a value into a String: String.valueOf(x) (works for anything), x.toString() on objects, and the shortcut x + "". A bonus of String.valueOf: for a null object it returns the text "null" instead of crashing.
String.valueOf(42); // "42"
Object o = null;
String.valueOf(o); // "null"
o.toString(); // NPE!+ reads left to right
When either side of + is a String, + means "concatenate" and the other side is converted to text. Java evaluates left to right: 1 + 2 + "3" does 1 + 2 = 3 first (two ints!), then 3 + "3" = "33".
String a = 1 + 2 + "3"; // "33"
String b = "1" + 2 + 3; // "123"Your turn
What does this print?
System.out.println(4 + 5 + "x");
System.out.println("x" + 4 + 5);9x x99x x4545x x45
Show the answer
9x, then x45. In the first line the two ints are added before a String appears. In the second, the String comes first, so each number is glued on as text.
Text into numbers
Integer.parseInt("42") returns an **int**; Integer.valueOf("42") returns an **Integer** object. Double.parseDouble("2.5") handles decimals. A leading minus works ("-17"), and so do leading zeros: "08" becomes 8.
int a = Integer.parseInt("-17"); // -17
Integer b = Integer.valueOf("08"); // 8
double c = Double.parseDouble("2.5");// 2.5Almost a number
What happens here?
int n = Integer.parseInt("3.0");
System.out.println(n);33.0Throws NumberFormatException
Show the answer
It throws NumberFormatException. parseInt accepts only an optional sign followed by digits — the dot makes it invalid. For decimals, use Double.parseDouble.
User input is hostile
Users type "1,000", " 42" or nothing at all. parseInt and valueOf reject all of these with NumberFormatException. Catch it (or clean the text first) and show a friendly message instead of crashing.
try {
int qty = Integer.parseInt(raw);
} catch (NumberFormatException e) {
showError("Please enter a number");
}In real projects
Every form field, URL parameter, config file and CSV column arrives as text. An unguarded parseInt on user input is one of the most common crashes in beginner apps — and in production logs. Validate at the boundary, convert once, then work with real numbers.
Key takeaways
- String.valueOf(x) works for any value, even null (gives "null")
- Integer.parseInt returns int; Integer.valueOf returns Integer
- Invalid number text throws NumberFormatException
- + is evaluated left to right: 1 + 2 + "3" is "33"
Since Java 7, parseInt also accepts a leading plus sign: Integer.parseInt("+5") returns 5.
Practice questions
What does this print?
int n = 7;
System.out.println(n + "" + 3);
System.out.println(1 + 2 + "3");- 10 33
- 73 33
- 73 123
- 10 123
Check your answer
73 33. + runs left to right. n + "" becomes "7", then + 3 gives "73". In the second line 1 + 2 is 3 first (both ints), then + "3" gives "33".
What does this print?
int x = Integer.parseInt("12.5");
System.out.println(x);- 12
- 13
- 12.5
- Throws NumberFormatException
Check your answer
Throws NumberFormatException. parseInt only accepts an optional sign followed by digits. "12.5" contains a dot, so it throws. Use Double.parseDouble for decimals.