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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  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.

Dustin Marx user avatar by
Dustin Marx
·
Aug. 28, 18 · Presentation
Like (24)
Save
Tweet
Share
79.94K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Gentle Introduction to Kubernetes
  • Build an Automated Testing Pipeline With GitLab CI/CD and Selenium Grid
  • How To Choose the Right Streaming Database
  • Seamless Integration of Azure Functions With SQL Server: A Developer's Perspective

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: