Unexpected recursion triggered by the toString () method in Java

Source: Internet
Author: User

Let's take a look at a simple java code:

ToString () InfiniteRecursion {String toString () {"" ++ "";} main (String [] args) {InfiniteRecursion obj = InfiniteRecursion (); System .. println (obj );}}

 

After running, we see a series of exception information, StackOverflow:

Stack explosion is usually caused by recursion or infinite loops. The direct cause of stack overflow caused by the code above is recursion. The following analyzes the cause of recursion:

The definition class InfiniteRecursion, which was originally intended to print the memory address of the current object through the toString method, will of course think of this keyword when obtaining the current object. In the toString method, the Java compiler finds that a String is followed by a plus sign, and the object after the plus sign is not of the String type. Therefore, the compiler tries to automatically convert this to the String type, that is, call this. the toString () method, but this refers to the object re-implemented the toString method, resulting in the loop continuously calling the toString method, so a recursive call occurs.

The above toString () is summed up by Bruce Eckel as unconscious recursion. It is also applicable in. NET (rewrite the ToString method) and I will not post similar code.

This kind of unconscious recursion is still caused by improper class inheritance. Maybe you will say that there is no technical difficulty at all. Isn't class inheritance right? Isn't it a ToString method inherited from the area of the Object?

If the code you maintain is a rich base class code, the inheritance chain is very deep, it contains a large number of packaging classes, and the class calling relationship is complex ...... It's easy to say. Let's talk about a real case that is caused by class inheritance:

The user reported that a page of the online system of an important business department of our company could not be opened normally. the intuitive effect is that the page could not be opened until loading. The developer spent A lot of effort. It is said that the Windbg and the legendary architect were used to solve the problem of calling A page method. To illustrate the problem, we temporarily named this page as, page A inherits from the custom base class page B, and the cause of the problem is roughly analyzed as follows:

1. func1 triggers the call of another func2 method of base class B on page;

2. When the method func2 in B is executed, it is found that it has been overwritten by A, so the method func2 of A rewrite is executed;

3. The func1 function is called inside the func2 function rewritten by A, so that the execution of the func1 function of A is re-triggered.

In this way, the unconscious recursion of a god is formed without knowing it.

This is the end of a classic case study of full and real side effects of class inheritance.

Tomorrow, will you still use class inheritance? Do you dare to use class inheritance? Do you still have the heart to use class inheritance?

 

Reference: <Thinking in Java>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.