Java String.valueOf: Converting Values to Strings
Learn how Java String.valueOf converts primitives and objects to strings, how its null handling works, and when to prefer it over other conversion methods.
Java's String.valueOf method is the standard way to convert a primitive value or an object into a String. The Object overload handles null references by returning the string "null" instead of throwing a NullPointerException, which makes it a safer choice when the input might be null. This article explains the overloads, null behavior, performance considerations, and when to prefer valueOf over other conversion approaches.
Overloads and Return Behavior
String.valueOf has overloads for boolean, char, int, long, float, double, char[], a char[] subrange (valueOf(char[], int, int)), and Object. byte and short arguments are widened to int before the int overload is used. Each overload normally returns a String representation of the argument. For primitives, the conversion does not create intermediate wrapper objects. For Object, valueOf returns the string "null" if the argument is null; otherwise it returns the result of calling toString() on the object.
| Input type | Example call | Result |
|---|---|---|
int | String.valueOf(42) | "42" |
boolean | String.valueOf(true) | "true" |
char | String.valueOf('a') | "a" |
double | String.valueOf(3.14) | "3.14" |
Object | String.valueOf((Object) null) | "null" |
char[] | String.valueOf(new char[]{'a','b'}) | "ab" |
These overloads are all static methods on String, so you call them directly on the class, not on an instance.
How String.valueOf Handles null
The most useful null-handling behavior is on valueOf(Object). If you pass a null reference, it returns the string "null" rather than throwing an exception. This is particularly helpful when building log messages or constructing user-facing output from data that may be incomplete. For example:
Object obj = null; String result = String.valueOf(obj); // returns "null"
The char[] overload is different. Passing a null array to String.valueOf(char[]) throws a NullPointerException; it does not return "null". This can be surprising because overload resolution makes a bare String.valueOf(null) call choose the char[] overload over the Object overload. To force the Object overload, cast the null literal or use an Object-typed variable:
String a = String.valueOf((Object) null); // "null" // String b = String.valueOf(null); // chooses char[], throws NullPointerException
Converting Primitives Without Autoboxing
When you call String.valueOf(int), the method directly converts the primitive to its decimal representation without boxing it into an Integer. For example:
int number = 42; String s = String.valueOf(number); // "42"
The other primitive overloads behave in the same way, and byte and short values are widened to int before conversion.
Using String.valueOf with char Arrays
The char[] overload is special. It returns a String containing the characters of the array, not the default object representation. For instance:
char[] chars = {'h', 'e', 'l', 'l', 'o'}; String s = String.valueOf(chars); // "hello"
This is useful when you have a character buffer and need a String copy. Note that this overload does not call toString() on the array; it directly constructs a String from the characters. If you accidentally cast the array to Object, you will invoke the Object overload and get the default array representation, which is not what you want.
Performance Considerations
String.valueOf is generally efficient for converting a single value. The primitive overloads avoid wrapper objects. String concatenation such as "" + value may build the result through a StringBuilder or through the JVM's invokedynamic-based concatenation, depending on the JDK version, so the allocation behavior is not always simple. For a single conversion, any difference is negligible. In a loop, valueOf may reduce allocations, but escape analysis can also remove some of the intermediate allocations. The more important reasons to prefer valueOf are clarity and the null-safe Object overload, not raw speed.
When to Prefer String.valueOf Over Other Conversions
Choose String.valueOf when you need to convert a primitive or when an object reference might be null and you still want a String result. For primitives, it is equivalent to calling the corresponding wrapper's toString, for example Integer.toString(i). For objects, it is equivalent to obj == null ? "null" : obj.toString(). If you know the object is never null, calling toString() directly is fine and slightly more direct. If you are building a string from multiple parts, string concatenation is often more readable. For a single conversion, valueOf is a clear and safe choice when the object reference can be null.
Common Pitfalls and Edge Cases
One pitfall is using valueOf on a char[] when you actually want the object representation. For example, String.valueOf((Object) new char[]{'a'}) will call the Object overload and return something like [C@15db9742 because it calls toString() on the array. The exact suffix depends on the hash code, so do not rely on its value. Another edge case is passing null as a char[]: overload resolution selects the char[] method, which throws a NullPointerException rather than returning "null".
Also, valueOf(char) returns a single-character string, not a number. And valueOf(float) and valueOf(double) may produce scientific notation for large or small numbers, consistent with Float.toString() and Double.toString(). Understanding these behaviors helps you avoid subtle bugs in conversion logic.