Recursion in JavaRecursion in java is a process in which a method calls itself continuously. A method in java that calls itself is called recursive method. It makes the code compact but complex to understand. Syntax: Java Recursion Example 1: Infinite timesOutput: hello hello ... java.lang.StackOverflowError Java Recursion Example 2: Finite timesOutput: hello 1 hello 2 hello 3 hello 4 hello 5 Java Recursion Example 3: Factorial NumberOutput: Factorial of 5 is: 120 Working of above program: factorial(5) factorial(4) factorial(3) factorial(2) factorial(1) return 1 return 2*1 = 2 return 3*2 = 6 return 4*6 = 24 return 5*24 = 120 Java Recursion Example 4: Fibonacci SeriesOutput: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263812.html