String.valueOf vs. Object.toString
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
String.valueOf(obj) and obj.toString() often produce the same visible text, but they are not equivalent operations. The important difference is null handling. String.valueOf(obj) is null-safe and returns the literal string "null" when the reference is null, while calling toString() on a null reference throws NullPointerException.
What Object.toString() Means
Every Java class inherits toString() from Object unless it overrides it.
If you call person.toString() on a non-null Person, Java invokes that instance method and returns the custom representation.
If the class does not override toString(), the default implementation from Object returns a string containing the class name and a hash-like suffix. That is usually not very useful for end users, but it is still a legitimate object string representation.
The key limitation is that instance methods require a real object reference.
This throws NullPointerException immediately.
What String.valueOf(obj) Does
String.valueOf is a static utility. For an object argument, it behaves conceptually like this:
- if the object is null, return
"null" - otherwise return
obj.toString()
This prints null instead of crashing.
That makes String.valueOf a safer default in formatting and logging code where null values can legitimately appear.
When the Results Are the Same
For non-null objects with a meaningful toString() implementation, both expressions usually produce the same string.
Both lines print the same value because String.valueOf(person) delegates to person.toString().
So the real difference is not formatting quality. It is defensive behavior when null is possible.
String.valueOf Also Works with Primitives
String.valueOf is overloaded for primitives as well.
That makes it convenient in generic conversion code. Primitives do not have instance methods, so you cannot write x.toString() for an int.
String Concatenation Uses Similar Logic
Java string concatenation often behaves in a null-safe way because it effectively converts operands to strings during concatenation.
This produces payload=null rather than throwing an exception. That is another reason String.valueOf feels natural: it matches how Java commonly treats string conversion in everyday code.
Which One Should You Use?
Use obj.toString() when:
- you know the reference cannot be null
- you want to call the instance method directly
- a null reference should be treated as a bug rather than a valid case
Use String.valueOf(obj) when:
- null is possible and safe conversion matters
- you are building log messages or debug strings
- you want one conversion utility that also handles primitives
In application code, String.valueOf is often the safer choice unless you intentionally want null to fail fast.
Common Pitfalls
The biggest mistake is calling toString() on a reference that may be null.
Another mistake is assuming String.valueOf(obj) ignores the object's custom toString() implementation for non-null objects. It does not; it delegates to it.
A third issue is forgetting that String.valueOf also supports primitives, which is one reason it appears so often in formatting code.
Summary
- '
obj.toString()requires a non-null object reference' - '
String.valueOf(obj)is null-safe and returns"null"for a null reference' - For non-null objects,
String.valueOf(obj)usually delegates toobj.toString() - '
String.valueOfalso supports primitives directly' - Use
String.valueOfwhen safety and generic conversion matter more than direct instance-method style
Related reading

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.