Below is a simple example of generating the first 10 numbers in the Fibonacci sequence using Java. If we want to adjust how many items we want to display, just modify the constant NUMBER_OF_ITEMS.

package test; /** * A simple example that displays the Fibonmacci sequence in Java using a for loop. */ public class FibonacciExample { private final static int NUMBER_OF_ITEMS = 10; public static void main(String[] args) { int firstNumber = 0; int secondNumber = 1; System.out.print(firstNumber); System.out.print(", "+secondNumber); for (int i = 3; i <= NUMBER_OF_ITEMS; ++i) { int nextNumber = firstNumber + secondNumber; System.out.print(", "+nextNumber); firstNumber = secondNumber; secondNumber = nextNumber; } } }

0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Sometimes we don't want to get the whole sequence or series, but just the nth term in the sequence. A function is a good way to do that, here is a simple example:

package test; /** * A simple example gets the nth term of a Fibonacci sequence: */ public class FibonacciExample { private static int getFibonacci(int term) { if (term == 1) { return 0; } if (term == 2) { return 1; } int firstNumber = 0; int secondNumber = 1; int nextNumber = 1; for (int i = 3; i <= term; ++i) { nextNumber = firstNumber + secondNumber; firstNumber = secondNumber; secondNumber = nextNumber; } return nextNumber; } public static void main(String[] args) { System.out.println(getFibonacci(1)); System.out.println(getFibonacci(2)); System.out.println(getFibonacci(3)); System.out.println(getFibonacci(4)); System.out.println(getFibonacci(5)); System.out.println(getFibonacci(6)); System.out.println(getFibonacci(7)); System.out.println(getFibonacci(8)); System.out.println(getFibonacci(9)); System.out.println(getFibonacci(10)); } }As you can see, we just modified our algorithm from the first example to come up with a function. And we try to display the first 10 items in the Fibonacci series in Java. The result is shown below as expected:

0 1 1 2 3 5 8 13 21 34

package test; /** * A simple example gets the nth term of a Fibonacci sequence using recursion: */ public class FibonacciExample { private static int getFibonacci(int term) { if (term == 1) { return 0; } if (term == 2) { return 1; } return getFibonacci(term - 1) + getFibonacci(term - 2); } public static void main(String[] args) { System.out.println(getFibonacci(1)); System.out.println(getFibonacci(2)); System.out.println(getFibonacci(3)); System.out.println(getFibonacci(4)); System.out.println(getFibonacci(5)); System.out.println(getFibonacci(6)); System.out.println(getFibonacci(7)); System.out.println(getFibonacci(8)); System.out.println(getFibonacci(9)); System.out.println(getFibonacci(10)); } }This is the beauty of recursion as the solution is close to how we describe a Fibonacci number. That is the first term is 0, the second is 1, and all the succeeding terms are produced by adding the previous two. getFibonacci(n) = getFibonacci(n - 1) + getFibonacci(n - 2). Recursion is actually much slower than the for loop solution, but it works and it is so pretty! The output is the same as shown below:

0 1 1 2 3 5 8 13 21 34