... fibonacci(1) is 1. Interestingly, if number is greater than 1, the recursion step generates two recursive calls (lines 15–16), each for a slightly smaller problem than the original call to fibonacci. Lines 15–16 use BigInteger methods add and subtract to help implement the recursive step. We also use a constant of type BigInteger named TWO that we defined at line 6.

Analyzing the Calls to Method Fibonacci

Figure 18.6 shows how method fibonacci evaluates fibonacci(3). At the bottom of the figure we’re left with the values 1, 0 and 1—the results of evaluating the base cases. The first two return values (from left to right), 1 and 0, are returned as the values for the calls fibonacci(1) and fibonacci(0). The sum 1 plus 0 is returned as the value ...

Get Java How To Program, Late Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.