Numbers & Strings
In short: Converting between number and text in both directions — number to String with String.valueOf(number) or concatenation, string to number with Integer.parseInt(text)/Double.parseDouble(text).
In more detail: parseInt/parseDouble throw a NumberFormatException if the string doesn’t contain a valid number format (e.g. letters or an empty string) — for user input, this is therefore usually guarded with a try/catch.
int number = Integer.parseInt("42");
String text = String.valueOf(42);In Depth
// Number -> text
int number = 42;
String a = String.valueOf(number); // "42"
String b = "" + number; // "42" - works, but uncommon/less clear
String c = Integer.toString(number); // "42"
// Text -> number, guarded against invalid input
String input = "abc";
try {
int value = Integer.parseInt(input);
} catch (NumberFormatException e) {
System.out.println("'" + input + "' is not a valid number");
}
// Wrapper classes offer the same parse methods for all numeric types
double d = Double.parseDouble("3.14");
long l = Long.parseLong("9999999999");
boolean isNumber = input.matches("-?\\d+"); // upfront check via regex instead of try/catchparseInt/parseDouble/etc. are among the most common sources of unhandled NumberFormatExceptions in beginner programs, especially when directly processing user input — a user who types letters instead of numbers otherwise crashes the program with an ugly stack trace message instead of showing an understandable error message. The combination of an upfront regex check (matches("-?\\d+") detects whether a string consists only of an optional minus sign plus digits) and a subsequent try/catch as additional protection is a robust, common pattern for input validation.
parseXxx() vs. valueOf() — the subtle difference
Every wrapper class offers both, with one important type difference: Integer.parseInt("42") returns a primitive int, Integer.valueOf("42") returns an Integer object. With automatic boxing, this often doesn’t matter in practice, but it’s relevant when an object is explicitly needed (e.g. for a collection like List<Integer>) instead of a primitive value — valueOf() also uses an internal cache for small numbers (-128 to 127), which can surprisingly return true for == comparisons between Integer objects in this range, while larger numbers don’t.
Printing numbers with formatting
For the reverse direction (number → readably formatted text), plain String.valueOf() is often not enough — thousands separators, fixed decimal places, or currency formats are handled by NumberFormat or String.format():
double price = 1234567.891;
System.out.println(String.format("%,.2f", price)); // "1,234,567.89" (US format)
NumberFormat german = NumberFormat.getInstance(Locale.GERMANY);
System.out.println(german.format(price)); // "1.234.567,891" (German format: dot/comma swapped)The locale dependency is a common pitfall in internationalized applications: the same number is displayed with different separator and decimal characters depending on the locale, which can cause confusion with hardcoded formatting when the target audience changes.
See also: String Concatenation, Type Casting, Exceptions