Java String toString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, String.toString() simply returns this: the very same string object, not a copy. A string is already a string, so there is nothing to convert.

The method exists because every class inherits toString() from Object, and code that works with Object or CharSequence variables calls it to get text. String overrides it so that such code receives the characters instead of a class name and hash.

toString() is an instance method of java.lang.String, available since JDK 1.0. Call it on a string, as in text.toString(); calling it on a null reference throws NullPointerException.

Syntax

Call it with no arguments. You rarely need it on a variable whose type is already String.

java
Object value = "text";
String shown = value.toString();      // "text"
CharSequence seq = "abc";
String plain = seq.toString();
CallWhat it does
"hi".toString()"hi", the same object.
s.toString() == strue for any non-null s.
String.valueOf(s)The null-safe alternative.

println and string concatenation call toString() for you on objects, which is why "value: " + obj works. For a null reference they print null instead of calling the method.

Library declarations
java
public String toString();

Parameters

ParameterDescription
NonetoString() takes no parameters.

Return Value

Return ValueDescription
StringThis string object itself.

Java Examples

Example 1 — toString() Returns the Same Object

Compare references to see that no copy is made.

java
public class ToStringSameObject {
    public static void main(String[] args) {
        String s = "hello";
        String t = s.toString();

        System.out.println(t);
        System.out.println("same object? " + (s == t));

        String copy = new String("hello");
        System.out.println("copy == original? " + (copy.toString() == s));
    }
}

How It Works

  1. No conversion needed. s.toString() returns the reference it was called on, so t prints "hello".
  2. Identity check. s == t is true: both variables point at one object.
  3. Each object returns itself. copy.toString() returns copy, a different object from the literal, so the last comparison is false.

Example 2 — Calling toString() Through Object and CharSequence

The same call works for many types; each class supplies its own text.

java
public class ToStringPolymorphism {
    public static void main(String[] args) {
        Object[] items = {"text", 42, 'c', 3.5};

        for (Object item : items) {
            String shown = item.toString();
            System.out.println(shown + " (" + item.getClass().getSimpleName() + ")");
        }

        CharSequence seq = "abc";
        System.out.println(seq.toString().length());
    }
}

How It Works

  1. Mixed values in one array. Autoboxing stores 42, 'c', and 3.5 as Integer, Character, and Double objects.
  2. Dynamic dispatch. item.toString() runs the override of each real class; for the String element it just returns the string.
  3. Get a String from an interface. A CharSequence variable needs toString() before String-only methods; here the text has length 3.

Example 3 — toString() Versus String.valueOf() for null

See which approaches survive a missing value.

java
public class ToStringNull {
    public static void main(String[] args) {
        String missing = null;

        System.out.println(String.valueOf(missing));
        System.out.println("joined: " + missing);
        try {
            System.out.println(missing.toString());
        } catch (NullPointerException e) {
            System.out.println("toString() on null: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. valueOf checks first. String.valueOf(Object) returns the text "null" when its argument is null.
  2. Concatenation is safe too. The + operator writes null for a null reference instead of calling a method on it.
  3. A direct call fails. missing.toString() needs an object to call the method on, so it throws NullPointerException.

Frequently Asked Questions

The string itself (this). No new object is created.
Every Java class inherits toString() from Object. String overrides it so code using Object or CharSequence references gets the text.
Not really. It is useful when the variable type is Object or CharSequence, or when the value comes from a generic API.
For a non-null object they give the same text. On null, toString() throws NullPointerException while String.valueOf(Object) returns "null".
Yes. println(Object) uses String.valueOf(obj), which calls toString() for non-null objects and prints null otherwise.

Next: toUpperCase()

Convert every letter to uppercase (pass a Locale to be safe).

toUpperCase() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful