Skip to content

A method that calls itself

Read · 1 of 3

Two parts

A recursive method solves a smaller version of the same problem, then finishes the job: 4! = 4 × 3!.

It needs a base case where the answer is known directly (1! = 1). Without it, it calls itself forever and crashes with StackOverflowError.