Java.lang Package Classes

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.

import java.lang.*;

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.lang - String