DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Generics in Java and Their Implementation
  • JSON-Based Serialized LOB Pattern
  • High-Performance Java Serialization to Different Formats
  • Writing DTOs With Java8, Lombok, and Java14+

Trending

  • Jakarta EE 12: Entering the Data Age of Enterprise Java
  • Reactive Ops to Autonomous Infrastructure: How Agentic AI Is Redefining Modern DevOps
  • Why Your RAG Pipeline Will Fail Without an MCP Server
  • How Reactive Scaling Drains Your Cloud Budget Without Warning
  1. DZone
  2. Data Engineering
  3. Data
  4. String.valueOf(Object) Vs. Objects.toString(Object)

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.

By 
Dustin Marx user avatar
Dustin Marx
·
Aug. 28, 18 · Presentation
Likes (27)
Comment
Save
Tweet
Share
87.0K Views

Join the DZone community and get the full member experience.

Join For Free

The 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)

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 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.

Data Types Strings Object (computer science)

Published at DZone with permission of Dustin Marx. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Generics in Java and Their Implementation
  • JSON-Based Serialized LOB Pattern
  • High-Performance Java Serialization to Different Formats
  • Writing DTOs With Java8, Lombok, and Java14+

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook