Why rewrite toString method in Java

Keywords: Attribute Java

When we create Java bean objects, we usually override the toString object method. Why?

1. The toString method is one of the methods in the Object class, which is used to return the string of the object.

Two. Why rewrite the toString method?

3. Code demonstration

// Create a Person class object without overwriting the toString method
public class Person  {
    private int age;
    private String name;
    private Integer height;

    public Person() {
    }

    public Person(int age, String name, Integer height) {
        this.age = age;
        this.name = name;
        this.height = height;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}



public class Main {
    public static void main(String[] args)  {
       Person p =new Person();
       // Print object
        System.out.println(p);
    }
}

The results are as follows: the address value of the object is printed

// Create a Person class object without overwriting the toString method
public class Person  {
    private int age;
    private String name;
    private Integer height;

    public Person() {
    }

    public Person(int age, String name, Integer height) {
        this.age = age;
        this.name = name;
        this.height = height;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // Rewriting toString method
    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", height=" + height +
                '}';
    }
}



public class Main {
    public static void main(String[] args)  {
       Person p =new Person();
       // Print object
        System.out.println(p);
    }
}

The result is as follows: Print the attribute value of the object

Parsing: Print the object, System.out.println(Object), which automatically calls Object.toString() when executing the println method. Print the address value of the object.

// Rewriting toString method
    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", height=" + height +
                '}';
    }

Override the toString method, we can get the attribute value of the object.

Posted by ercantan on Tue, 08 Oct 2019 09:06:58 -0700