Java Recursion
A java recursion is a method that calls itself over and over again and again. It's a different methods of doing a loop. Lets see a simple java recursion that finds a factorial.
ClickHere to download Factorial.java
In the example above the factorial is called with whatever argument passed into n. Although, return n * factorial(n-1) is a return statement but it does not return something immediately. The return statement will be call recursively until we reach to the base case which is n == 0. When the parameter reaches to 0, the method returns a value without making another recursive call.
ClickHere to download RecursiveSum.java
The above example, we have a base case for start == end, so the recursive method will end when we have the start index equals to the end index. Note: the start index for array is always 0, and the end index is whatever the size of the array.
If we enter 3, the size of the array is 3. In the recursive method, start increases by 1, then once it reaches to 3 which is our base case, the recursive method ends.