Java String toString() Method Tutorial

In this section, we will learn what the String toString() method is and how to use it in Java.

What is Java String toString() Method?

The `toString()` method is defined in the `Object` class and the `String` class has the overridden version of this method. It simply returns the String object that calls this method.

Note: because this method is implicitly called, we don’t need to call it when we want to get a string object.

Java toString() Method Syntax:

public String toString()

toString() Method Parameters:

This method does not take an argument.

toString() Method Return Value:

The return value of this method is the string object that this method is called with.

toString() Method Exceptions:

The method does not throw an error.

Example: using String toString() method

public class Simple {

    public static void main(String[] args)  {
        String w = "Nobody ever drowned in his own sweat";
        System.out.println(w.toString());
        System.out.println(w);
    }
}

Output:

Nobody ever drowned in his own sweat

Nobody ever drowned in his own sweat

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies