In Java, Object class is the super class of every class. If toString() method is not overriden in that particular class , then Object class toString() method will be the default implementation for that class.

So, the default implementation of toString() method in Object class is as below:

    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

toString() method is overridden in String class. The implementation of toString() method in String class is different from default implementation of toString() method in Object class.

So, the implementation of toString() method in String class is as below:

    public String toString() {
        return this;
    }

toString() method in String class returns the String itself which means returned String points to the same object on which toString() method is called.

Let’s take few examples to understand this.

Example 1: String toString() Method
package com.masterjavatutorial;
public class TestExample {
	public static void main(String[] args) {
		String str1 ="HELLOWORLD";
		String st =str1.toString();
		System.out.println(st);
		System.out.println("st and str1 are same???"+ (st== str1)); 
		
	}
}

Output of the above program is as below:

HELLOWORLD
st and str1 are same???true

Here, st and str1 are same as it is indicated by the fact that both references are pointing to the same object. Here, st and str1 are created in String contant pool memory area.

Example 2: String toString() Method
package com.masterjavatutorial;
public class TestExample {
	public static void main(String[] args) {
		String str1 = new String("HELLOWORLD");
		String st =str1.toString();
		System.out.println(st);
		System.out.println("st and str are same???"+ (st== str1)); 
		
	}
}

Output of the above program is as below:

HELLOWORLD
st and str1 are same???true

Here, st and str1 are same as it is indicated by the fact that both references are pointing to the same object. Here, st and str1 are created in Heap memory area.

Example 3: String toString() Method
package com.masterjavatutorial;
public class TestExample {
	public static void main(String[] args) {
		String str1 = new String("HELLOWORLD"); //Created in Heap area
		String str2 ="HELLOWORLD"; // created in constant pool area
		
		String str3 = str1.toString();
		String str4 = str2.toString();
		
		System.out.println("str3=="+str3);
		System.out.println("str4=="+str4);
		System.out.println("str3 and str4 are same???"+ (str3== str4)); 
		
	}
}

Output of the above program is as below:

str3==HELLOWORLD
str4==HELLOWORLD
str3 and str4 are same???false

Here, str3 and str4 both have the value HELLOWORLD, still both point to different object.

str3 points to the same object str1 in Heap Memory area .
str4 points to the same object str2 in String constant pool area.

So, both objects are in two different memory area.