🔤 Strings & Text · Beginner

Converting to and from Strings in Java

String.valueOf, toString, Integer.parseInt, NumberFormatException.

🧩 The mystery"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"
🔮 Predict it

Your turn

What does this print?

System.out.println(4 + 5 + "x");
System.out.println("x" + 4 + 5);
  1. 9x x9
  2. 9x x45
  3. 45x 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.5
🔮 Predict it

Almost a number

What happens here?

int n = Integer.parseInt("3.0");
System.out.println(n);
  1. 3
  2. 3.0
  3. Throws 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.

⚠️ The trap

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 the real world

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

  1. String.valueOf(x) works for any value, even null (gives "null")
  2. Integer.parseInt returns int; Integer.valueOf returns Integer
  3. Invalid number text throws NumberFormatException
  4. + is evaluated left to right: 1 + 2 + "3" is "33"
🤯 Did you know?

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");
  1. 10 33
  2. 73 33
  3. 73 123
  4. 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);
  1. 12
  2. 13
  3. 12.5
  4. 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.

Next: the "billion-dollar mistake" — null — and how to compare Strings without crashing.