Unconscious recursion of Java strings

Keywords: Java

Every class in Java basically inherits from Object, and standard container classes are naturally no exception. So container classes all have toString() methods and override them so that the String results generated by them can express the container itself and the objects contained in the container. For example, ArrayList.toString(), which traverses all the objects contained in ArrayList, calls the toString() method on each element:

public class Person {
    
    private int age;
    
    private String name;

    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 Person(int age, String name) {
        super();
        this.age = age;
        this.name = name;
    }
    
    @Override
    public String toString() {
        return "Person [age=" + age + ", name=" + name + "]";
    }

    public static void main(String[] args) {
        ArrayList<Person> persons = new ArrayList<>();
        Person p1 =  new Person(10, "Zhang San");
        Person p2 =  new Person(12, "Li Si");
        persons.add(p1);
        persons.add(p2);
        //(1)and(2)The output is the same
        System.out.println(persons);//(1)
        System.out.println(persons.toString());//(2)
    }
}

The output results are as follows:

[Person [age=10, name = Zhang San], Person [age=12, name = Li Si]]
[Person [age=10, name = Zhang San], Person [age=12, name = Li Si]]

 

If you want the toString() method to print out the object's memory address, you might consider using this keyword:

public class Person {
    
    private int age;
    
    private String name;

    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 Person(int age, String name) {
        super();
        this.age = age;
        this.name = name;
    }
    
    @Override
    public String toString() {
        return "Person address:"+this;
    }

    public static void main(String[] args) {
        ArrayList<Person> persons = new ArrayList<>();
        Person p1 =  new Person(10, "Zhang San");
        Person p2 =  new Person(12, "Li Si");
        persons.add(p1);
        persons.add(p2);
    
        System.out.println(persons);//(1)
        System.out.println(persons.toString());//(2)
    }
}

When you create a Person object and print it out, you get a very long list of exceptions. If you save the Person object in an ArrayList and print the ArrayList, you will get the same exception. Actually, when the following code runs:

"Person address:" + this

There is automatic type conversion, with Person type converted to String type. Because the compiler sees a String object followed by a "+", and then the object behind it is not String, the compiler tries to convert this into a String. How does it change? It is by calling the toString() method on this that recursive calls occur.

If you really want to print out the memory address of an object, you should call the Object.toString() method, which is the method responsible for this task. So instead of using this, you should call the super.toString() method.

That is to say, the above toString() method is changed to:

@Override
    public String toString() {
        return "Person address:" + super.toString();
    }

 

Welcome to the Wechat Public Number (Java Classics) and watch more Java technology products!

_Wechat Scanning and Scanning Graph__Two-Dimensional Code Concern

 

Posted by jtymes on Sat, 07 Sep 2019 01:00:31 -0700