String.valueOf() / Integer.toString()
Methods for converting numeric and other types to strings, or strings to numeric types. Type conversion is frequently needed in Java for input/output processing and string concatenation.
Syntax
// Converts a number, boolean, or other type to a string. String.valueOf(int value); String.valueOf(double value); String.valueOf(boolean value); String.valueOf(char value); // Converts an int to a string. Integer.toString(int i); Integer.toString(int i, int radix); // Converts using the specified radix. // Converts a double to a string. Double.toString(double d); // Converts a string to an int. Integer.parseInt(String s); // Converts a string to a double. Double.parseDouble(String s);
Method List
| Method | Description |
|---|---|
| String.valueOf(value) | Converts the given value to a string and returns it. If null is passed, it returns the string "null". |
| Integer.toString(int i) | Converts an int value to its decimal string representation and returns it. |
| Integer.toString(int i, int radix) | Converts an integer to a string using the specified radix (2–36). Useful for binary and hexadecimal conversions. |
| Double.toString(double d) | Converts a double value to a string and returns it. |
| Integer.parseInt(String s) | Converts a string to an int. Throws an exception if the string contains non-numeric characters. |
| Double.parseDouble(String s) | Converts a string to a double. |
Sample Code
// Converts an int to a string.
int num = 42;
String str1 = String.valueOf(num);
String str2 = Integer.toString(num);
System.out.println(str1); // Outputs: 42
System.out.println(str2); // Outputs: 42
// Converts a double to a string.
double price = 9.99;
System.out.println(String.valueOf(price)); // Outputs: 9.99
// Converts a boolean to a string.
System.out.println(String.valueOf(true)); // Outputs: true
// Converts a string to an int.
String age = "25";
int ageNum = Integer.parseInt(age);
System.out.println(ageNum + 1); // Outputs: 26
// Converts a string to a double.
String weight = "65.5";
double w = Double.parseDouble(weight);
System.out.println(w * 2); // Outputs: 131.0
// Converts to binary and hexadecimal.
System.out.println(Integer.toString(255, 2)); // Outputs: 11111111 (binary)
System.out.println(Integer.toString(255, 16)); // Outputs: ff (hexadecimal)
// Parsing a non-numeric string throws an exception.
try {
int x = Integer.parseInt("abc"); // Throws NumberFormatException.
} catch (NumberFormatException e) {
System.out.println("Cannot convert to a number.");
}
Notes
String.valueOf() is a general-purpose method that can convert any type to a string. Internally, it calls each wrapper class's toString() method, so the behavior is nearly identical. If you only need to concatenate a number with a string, you can use the + operator (e.g., "Value: " + num) and the compiler will convert it automatically, but String.valueOf() makes the conversion more explicit and readable.
Integer.parseInt() and Double.parseDouble() throw a NumberFormatException if the string contains non-numeric characters. Always use a try-catch block to handle errors when converting user input.
To compare strings, use equals() / compareTo(). To convert a string to a character array, use toCharArray().
If you find any errors or copyright issues, please contact us.