1. The object class is defined with the public String toString () method whose return value is a String type that describes the information about the current object;
2. The toString () method of the object class is automatically invoked when a String is connected to other types of data, such as SYSTEM.OUT.PRINTLN ("info" +person);
3. You can override the ToString () method on a user-defined type as needed, such as:
Class Dog {public String toString () {
Return "I ' m a good dog!!!"; }}class Test {public static void main (string[] args) {Dog d = new Dog (); System.out.println ("D: =" +d); }}//operation Result: D: = I ' m a good dog!!!
If the ToString () method is not overridden in the Dog class, it is:
Class Dog {//Public String toString () {//return "I ' m a good Dog!!!"; /}}class Test {public static void main (string[] args) {Dog d = new Dog (); System.out.println ("D: =" +d); }}//operation result is: D: = [email protected]
The return result is: class name [email protected]+ hash code
The ToString method in Java