Java
Programming
String Manipulation
Java Methods
Object Oriented Programming

String.valueOf vs. Object.toString

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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.

java
1class Person {
2    private final String name;
3
4    Person(String name) {
5        this.name = name;
6    }
7
8    @Override
9    public String toString() {
10        return "Person{name='" + name + "'}";
11    }
12}

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.

java
Person person = null;
System.out.println(person.toString());

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()
java
Person person = null;
System.out.println(String.valueOf(person));

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.

java
Person person = new Person("Alice");
System.out.println(person.toString());
System.out.println(String.valueOf(person));

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.

java
1int x = 42;
2double y = 3.14;
3boolean ok = true;
4
5System.out.println(String.valueOf(x));
6System.out.println(String.valueOf(y));
7System.out.println(String.valueOf(ok));

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.

java
Object payload = null;
String message = "payload=" + payload;
System.out.println(message);

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 to obj.toString()
  • 'String.valueOf also supports primitives directly'
  • Use String.valueOf when safety and generic conversion matter more than direct instance-method style

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.