In Java, String.valueOf(x) is a static method that returns the text form of almost any value: int, long, double, boolean, char, a char[], or any object.
For an object it returns obj.toString(), or the four-letter text "null" when the reference is null, so it is the safe way to print or log values that may be missing. For a char[] it copies the characters into a new String.
valueOf() is a static method of java.lang.String, available since JDK 1.0. Call it on the class name, as in String.valueOf(...). No import is needed.
Syntax
Call it on the class name and pass the value to convert.
java
String a = String.valueOf(42); // "42"
String b = String.valueOf(true); // "true"
String c = String.valueOf(chars, 0, 3); // first three chars
Call
What it does
String.valueOf(7)
"7".
String.valueOf(0.1 + 0.2)
"0.30000000000000004".
String.valueOf('A')
"A".
String.valueOf((Object) null)
"null".
Numbers use the same text as Integer.toString, Double.toString, and so on, which never depends on the locale: 2.5 always prints with a dot, and large doubles switch to 1.0E7 style.
Library declarations
java
public static String valueOf(Object obj);
public static String valueOf(char[] data);
public static String valueOf(char[] data, int offset, int count);
public static String valueOf(boolean b);
public static String valueOf(char c);
public static String valueOf(int i);
public static String valueOf(long l);
public static String valueOf(float f);
public static String valueOf(double d);
Parameters
Parameter
Description
obj, b, c, i, l, f, d
The value to convert.
data
A char array whose characters are copied.
offset, count
For the range overload: first index and number of chars to copy.
Return Value
Return Value
Description
String
The text form of the argument, or "null" for a null object reference.
Java Examples
Example 1 — Convert Primitive Values to Text
One overload per primitive type, all with the same simple call.
java
public class ValueOfPrimitives {
public static void main(String[] args) {
System.out.println(String.valueOf(42));
System.out.println(String.valueOf(2.50));
System.out.println(String.valueOf(1e7));
System.out.println(String.valueOf(true));
System.out.println(String.valueOf('x'));
System.out.println(String.valueOf(10L).length());
}
}
Output
42
2.5
1.0E7
true
x
2
How It Works
Overload by type. The compiler chooses valueOf(int), valueOf(double), valueOf(boolean), or valueOf(char) from the argument.
Double formatting rules.2.50 loses its trailing zero, and ten million switches to scientific notation 1.0E7.
A real String comes back.String.valueOf(10L) is "10", so String methods such as length() work on it and return 2.
Example 2 — Build Strings from a char Array
Convert a whole array and a range, then change the array afterwards.
java
public class ValueOfCharArray {
public static void main(String[] args) {
char[] letters = {'J', 'a', 'v', 'a', '!'};
String all = String.valueOf(letters);
String some = String.valueOf(letters, 0, 4);
letters[0] = 'L';
System.out.println(all);
System.out.println(some);
System.out.println(new String(letters));
}
}
Output
Java!
Java
Lava!
How It Works
Whole array.String.valueOf(letters) copies all five chars into "Java!".
A range by offset and count.valueOf(letters, 0, 4) takes four chars from index 0, leaving out the '!'.
The strings are copies. Changing letters[0] to 'L' affects only the array, which now reads "Lava!".
Example 3 — null Objects Versus null char Arrays
An object reference and a char array behave differently when they are null.
Object overload is null-safe. For a nullObject, valueOf returns the text "null" instead of throwing.
It is real text. The returned "null" is a normal four-character string, so length() prints 4.
char[] has no such check. The array overload reads the array immediately, so a nullchar[] throws NullPointerException.
Frequently Asked Questions
It converts a value of almost any type (primitives, char arrays, objects) into its String form.
Written literally, String.valueOf(null) calls the char[] overload and throws NullPointerException. A null Object variable, or (Object) null, returns "null".
valueOf returns "null" for a null reference, while obj.toString() throws NullPointerException. For non-null objects they give the same text.
Yes. String.valueOf(int) returns Integer.toString(i). Both produce the same digits.
No. The characters are copied, so later changes to the array do not affect the String.