ToString () is an Object class method that can be overridden by anyone who inherits an Object class. Printing an Object calls the Object’s toString() method by default. ToString (); toString(); toString();

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

We get the class name @+ the address name (hash code) so we usually override toString() to print out what we want to see. The code:

public class InFo { private String name; private Integer age; Public InFo(){/** Not writing no-argument constructs defaults to no-argument constructs for this class. If you just write with parameters, if you don't write with no parameters then you don't have any parameterless structures. When called, only the constructor with parameters is called, which can cause a lot of trouble later. Usually, the constructor with no parameters is written. */ system.out. println("InFo with no arguments "); } public InFo(String name,Integer age){ super(); This. name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } @Override public String toString() { return "InFo{" + "name='" + name + '\'' + ", age=" + age + '}'; } public void setAge(int age) { this.age = age; } public static void main(String[] args) {/** * Java dictionary JDK API * toString() returns a String of this object indicating ** ** / InFo InFo = new InFo (" bill ", 20); InFo info1 = new InFo(); System.out.println(info); // System.out.println(info.getName()+"----"+info.getAge()); System.out.println(info.toString()); /** info prints the same result as info.tostring. The default for printing an object is to call the object's toString() method. * * /}}Copy the code

Code running results: