String.valueOf(Object) Vs. Objects.toString(Object)
Are these two methods really all that different? Or, are they just similar with specific use cases? Click here to find out more about the string.valueOf() and Objects.toSpring() methods.
Join the DZone community and get the full member experience.
Join For FreeThe useful method String.valueOf(Object) has been around since JDK 1.0 and is one of the JDK-provided methods that I use on a regular basis. The Objects class was introduced with JDK 1.7 and included the Objects.toString(Object) method. This post compares these two similar methods. Let's get to it!
Both methods String.valueOf(Object)
and Objects.toString(Object)
essentially do the same thing: call the toString() method on a passed-in object. This is the case as long as it's not null
or it doesn't return the string "null" when null
is passed to them. In short, both methods are designed to provide a simple approach for invoking an object's toString()
without worry about a NullPointerException
if it turned out to be null
.
The table below compares characteristics of the methods String.valueOf(Object)
and Objects.toString(Object)
.
CHARACTERISTIC | STRING.VALUEOF(OBJECT) |
OBJECTS.TOSTRING(OBJECT) |
---|---|---|
Java SE 10 Method Javadoc | "Returns the string representation of the Object argument." | "Returns the result of calling toString for a non-null argument and 'null' for a null argument." |
Java SE 10 Return Javadoc | "if the argument is null , then a string equal to "null"; otherwise, the value of obj.toString() is returned." |
"the result of calling toString for a non-null argument and "null" for a null argument" |
Method Modifiers | public static |
public static |
Overloaded Versions | valueOf(boolean) valueOf(char) valueOf(char[], int, int) valueOf(double) valueOf(float) valueOf(int) valueOf(long) valueOf(Object) |
On the surface, it appears that String.valueOf(Object)
and Objects.toString(Object)
do the same thing. It turns out that they are the same. Here is the code for Objects.toString(Object)
from OpenJDK:
OpenJDK: Definition of Objects.toString(Object)
public static String toString(Object o) {
return String.valueOf(o);
}
The code snippet above shows that the Objects.toString(Object)
method simply delegates to String.valueOf(Object)
method. The OpenJDK implementation of String.valueOf(Object)
is shown next.
OpenJDK: Definition of String.valueOf(Object)
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
The above code snippets show that either String.valueOf(Object)
or Objects.toString(Object)
can be called when an object's toString()
representation is desired without risk of a NullPointerException
. There might be minor reasons to prefer one over the other, and I typically choose the direct call to String.valueOf(Object)
over the indirect call to that method via Objects.toString(Object)
.
Although I typically use String.valueOf(Object)
instead of Objects.toString(Object)
by default when I want the string "null" returned if the passed-in object is null
, the alternate overloaded method Objects.toString(Object, String) has the advantage of specifying any string to be returned by the method if the passed-in object is null
. So, for example, one could use this method to return an empty string (""
), and the string "nil", the string "none", or any other arbitrary string if a passed-in object was null
. The OpenJDK code listing for Objects.toString(Object, String)
is shown next.
OpenJDK: Definition of Objects.toString(Object, String)
public static String toString(Object o, String nullDefault) {
return (o != null) ? o.toString() : nullDefault;
}
One thing to note regarding the Objects.toString(Object, String)
implementation is that if one passes null
to this method as the second argument, then null
(rather than the string "null" or any other string) will be returned by that method.
The methods String.valueOf(Object)
, Objects.toString(Object)
, and Objects.toString(Object, String)
make it easy to provide string representations of objects without the need to write explicit checks for the null
.
Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments