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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Generics in Java and Their Implementation
  • JSON-Based Serialized LOB Pattern
  • High-Performance Java Serialization to Different Formats
  • How to Use State Inside of an Effect Component With ngrx

Trending

  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Automatic Code Transformation With OpenRewrite
  • How to Convert XLS to XLSX in Java
  • Integrating Security as Code: A Necessity for DevSecOps
  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 (25)
Comment
Save
Tweet
Share
85.8K 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.

Related

  • Generics in Java and Their Implementation
  • JSON-Based Serialized LOB Pattern
  • High-Performance Java Serialization to Different Formats
  • How to Use State Inside of an Effect Component With ngrx

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!