String conversion is the process of turning values from other data types into text, or converting text into another required type. In Java, this happens constantly because applications display numbers, read user input, build messages, store data, and communicate with external systems.
The important point is that String is not a primitive type. It is a class, so converting values to and from strings follows rules that are different from primitive numeric casting.
Important: You cannot convert a String to a numeric primitive using a simple cast such as (int) text. Parsing methods are used instead.
Why Is String Conversion Important?
Imagine a web form where a user enters an age. The keyboard input arrives as text, even though the application eventually needs an integer for calculations.
String input = "25"; int age = Integer.parseInt(input); System.out.println(age + 5);
The text "25" is parsed into the numeric value 25, allowing arithmetic to be performed.
This pattern appears everywhere in real applications: user input, configuration files, HTTP parameters, CSV data, environment variables, and database values often begin as text and must be converted into useful application types.
Primitive Value to String
Java provides several ways to convert primitive values into strings. One of the clearest approaches is String.valueOf().
int number = 150; String text = String.valueOf(number); System.out.println(text);
The resulting value is the string "150". Notice that text is now a String, not an integer.
Using Integer.toString()
For a specific numeric wrapper type, you can also use its toString() method.
int number = 250; String text = Integer.toString(number); System.out.println(text);
This approach clearly communicates that an integer is being converted into its textual representation.
Using String Concatenation
Java also converts values to strings automatically when they are concatenated with a string using the + operator.
int score = 95; String message = "Score: " + score; System.out.println(message);
The result is "Score: 95". Java converts the numeric value into text as part of string concatenation.
Remember: Once one side of a + expression is a String, Java performs string concatenation rather than ordinary numeric addition for the relevant operation.
Order Matters in String Concatenation
The position of the string can completely change the result.
System.out.println(10 + 20 + " Java"); System.out.println("Java " + 10 + 20);
The first expression produces "30 Java" because 10 + 20 is evaluated numerically before the string is encountered.
The second produces "Java 1020" because once the string appears, the following values are concatenated as text.
Common interview trap: Do not evaluate a mixed + expression from left to right without considering when the expression becomes string concatenation.
String to int
To convert a string containing a valid integer representation into an int, use Integer.parseInt().
String text = "500"; int number = Integer.parseInt(text); System.out.println(number + 100);
The result is 600. The string has been parsed into an integer.
Invalid Numeric String
Parsing is not the same as casting. Java must actually interpret the characters as a valid number. If the text does not represent a valid integer, parsing fails.
String text = "500px"; // Causes NumberFormatException: // int number = Integer.parseInt(text);
The exception is NumberFormatException. This is an important distinction when processing external input because users and external systems do not always provide perfectly formatted values.
String to long
String text = "9876543210"; long value = Long.parseLong(text); System.out.println(value);
Use Long.parseLong() when the textual value represents a long.
String to double
String text = "45.75"; double value = Double.parseDouble(text); System.out.println(value);
The text is parsed into a floating-point number. Similar parsing methods exist for other primitive numeric types.
| Target Type | Parsing Method | Example |
|---|---|---|
| byte | Byte.parseByte() | Byte.parseByte("10") |
| short | Short.parseShort() | Short.parseShort("10") |
| int | Integer.parseInt() | Integer.parseInt("10") |
| long | Long.parseLong() | Long.parseLong("10") |
| float | Float.parseFloat() | Float.parseFloat("10.5") |
| double | Double.parseDouble() | Double.parseDouble("10.5") |
String to boolean
Boolean conversion has slightly different behavior from numeric parsing.
String text = "true"; boolean enabled = Boolean.parseBoolean(text); System.out.println(enabled);
The result is true. The method recognizes the textual representation of true without requiring an exact uppercase or lowercase match.
Unlike numeric parsing, arbitrary text does not produce a NumberFormatException. For example, a non-true value results in false.
boolean value = Boolean.parseBoolean("hello"); System.out.println(value);
The result is false.
String to char
A string cannot be directly converted to a char using a cast. Instead, you normally retrieve a character from the string using charAt().
String text = "Java"; char first = text.charAt(0); System.out.println(first);
The result is 'J'. The index starts at zero, so the first character is located at index 0.
char to String
A character can be converted into a string using String.valueOf().
char letter = 'J'; String text = String.valueOf(letter); System.out.println(text);
Another common approach is concatenating the character with an empty string, although String.valueOf() communicates the intention more clearly.
Parsing and Casting Are Different
This distinction is one of the most important ideas in string conversion.
String text = "100"; int number = Integer.parseInt(text);
Here, Java reads the characters '1', '0', and '0' and interprets them as the numeric value 100.
A cast works differently:
double value = 100.75; int number = (int) value;
The second example converts one primitive numeric type into another. It does not interpret text.
Whitespace and Input
When strings come from user input or external sources, unwanted whitespace can prevent parsing from behaving as expected. A common solution is to trim surrounding whitespace before parsing.
String input = " 42 "; int number = Integer.parseInt(input.trim()); System.out.println(number);
The surrounding spaces are removed before the text is parsed.
String Conversion with null
One useful reason to understand String.valueOf() is its behavior with object references. When the argument is null, the object-oriented overload can produce the text "null".
String text = null; String result = String.valueOf(text); System.out.println(result);
The result is the four-character string "null".
This differs from directly calling toString() on a null reference, which would cause a NullPointerException.
Practical tip: When converting a possibly null object reference into display text, String.valueOf() can be safer than calling toString() directly.
Common Mistakes
- Trying to cast a String directly to int.
- Forgetting that parsing can fail when input does not contain a valid numeric representation.
- Assuming "10" and 10 are the same type.
- Forgetting that 10 + 20 + "Java" and "Java" + 10 + 20 produce different results.
- Using charAt() without checking whether the string contains a character at the requested index.
- Calling toString() on a reference that may be null.
Best Practices
- Use String.valueOf() when you want a clear and general value-to-string conversion.
- Use the appropriate parseXxx() method when converting validated text into primitive numeric values.
- Validate external input before relying on its format.
- Handle NumberFormatException when invalid numeric input is possible.
- Use charAt() only when you actually need an individual UTF-16 code unit from a string.
Interview Insight
A common interview question is: Can you convert a String to int using casting? No. A String is an object, while int is a primitive type. The text must be parsed, typically with Integer.parseInt().
| Conversion | Recommended Approach | Important Point |
|---|---|---|
| int → String | String.valueOf() | Produces textual representation. |
| double → String | String.valueOf() | Converts the numeric value to text. |
| String → int | Integer.parseInt() | Invalid input can throw NumberFormatException. |
| String → long | Long.parseLong() | Parses a long representation. |
| String → double | Double.parseDouble() | Parses floating-point text. |
| String → boolean | Boolean.parseBoolean() | Recognizes the textual true value. |
| String → char | charAt() | Retrieves a UTF-16 code unit at an index. |
| char → String | String.valueOf() | Creates a string containing the character. |
String conversion connects the human-readable world of text with the strongly typed world of Java values. The most important distinction to remember is that converting a value into text is different from parsing text into a value. Once you understand that difference, methods such as String.valueOf(), Integer.parseInt(), and charAt() become natural tools rather than methods you simply memorize.
