Recursion Basics:

  • A method that calls itself during its execution
  • Needs three key components:
    • Should have a terminating logic or condition
      • Can cause a StackOverFlowError
    • A reduction step
    • A recursive call

Factorial Example:

//Add negative check to this code to avoid a StackOverFlowError
public static int factorial(int n) {
   if (n == 0) {
       return 1;
   }
   else {
       return n * factorial(n-1);
   }
}