Why doesn't mapping in Eclipse support recursive functions in Java?

I implemented a factorial function in Java in the Eclipse IDE.

public class Utilities { public static int factorial(int n) { int result; if (n <= 1) // base case return 1; else { result = (n * factorial(n - 1)); return result; } } } 

On the display in Eclipse, I check Utilities.factorial(6) . However, I get this error.

 Utilities.factorial(6); Evaluation failed. Reason(s): Cannot perform nested evaluations. 

Why does the display not support recursive calls? This is problem?

+6
source share
3 answers

To return an answer, he must evaluate the expression, do it so that he evaluates the internal expression, do it so that he evaluates the internal expression, do it so that he evaluates the internal expression.

Usually, when the debugger hits the stack (there are too many nested frames of the stack), people send errors to the development team that writes the debugger. They fix the problem using only the means known at that time: no recursion.

If you could evaluate how deep the stack should go without evaluating the expression in a nested manner, for any recursive expression; A brilliant Fields Medal awaits there (and probably the university is ready to build a new building in your name). This problem is connected with the stopping problem , and, unfortunately, with our computational model it is known that the stopping problem is unsolvable.

+9
source

The Culprit: Recursion

Most IDEs will not do this kind of evaluation, it has nothing to do with Java and is more related to recursion. Since Eclipse cannot understand how deep the recursion is, it does not even evaluate the expression, otherwise it could hang the JVM (if recursion was an error) or throw an exception from the heap, from which it would be difficult to recover from anyway.

Have you tried lower numbers for a sentinel, for example. Utilities.factorial(1) ?

+4
source

Unrelated: If this is the method you really want to use, recursion is not the way to go. At a minimum, rewrite the function as iterative. Depending on your use case, you can use various approximations, such as this or this .

-1
source

Source: https://habr.com/ru/post/915836/


All Articles