Java Tutorial Java Advanced Java Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Java - String toString() Method



The Java string toString() method returns the object (which is already a string!) itself.

Syntax

public String toString()

Parameters

No parameter is required.

Return Value

Returns the string itself.

Example:

In the example below, toString() method returns the string object itself.

public class MyClass {
  public static void main(String[] args) {
    String MyString = "Hello World";
    String NewString = MyString.toString();

    System.out.println(NewString);
  }
}

The output of the above code will be:

Hello World

❮ Java String Methods

5