Core Java

String.valueOf(Object) versus Objects.toString(Object)

The handy method String.valueOf(Object) has been around since JDK 1.0 and is one of the JDK-provided methods that I use on a frequent basis. I have blogged on its value before. The Objects class was introduced with JDK 1.7 and included the Objects.toString(Object) method. This post compares these two similar-sounding methods.

Both methods String.valueOf(Object) and Objects.toString(Object) essentially do the same thing: call the toString() method on a passed-in object if it’s not null or 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).

CharacteristicString.valueOf(Object)Objects.toString(Object)
Java SE 10 Method JavadocReturns 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 Modifierspublic staticpublic static
Overloaded VersionsvalueOf​(boolean)
valueOf​(char)
valueOf​(char[], int, int)
valueOf​(double)
valueOf​(float)
valueOf(int)
valueOf(long)
valueOf(Object)
toString(Object)
toString(Object, String)

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 allowing one to specify 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 empty string (""), 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 null.

Published on Java Code Geeks with permission by Dustin Marx, partner at our JCG program. See the original article here: String.valueOf(Object) versus Objects.toString(Object)

Opinions expressed by Java Code Geeks contributors are their own.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Jarrod Roberson
Jarrod Roberson
6 years ago

Guava has a MoreObjects.firstNonNull(arg1, arg2); that I like better than the String one because it is more explicit in what its intent and function is and is more consistent when you need to replace nulls with any default replacement so you can static import it and just use firstNonNull() to be more succinct.

https://google.github.io/guava/releases/21.0/api/docs/com/google/common/base/MoreObjects.html

Dean
6 years ago

Kotlin:

object?.toString() ?: “Default”

Or to get “null”:

object.toString()

Java… Why bother?

Back to top button