What is the purpose of toString() method? Or What does the toString() method do in java?



The toString() method returns the current object in String format.

Example

Live Demo

public class Test {
public static void main(String args[]) {
Test obj = new Test();
System.out.println(obj.toString());
System.out.println("Hello");
}
}

Output

Test@2a139a55
Hello

Advertisements