Java recursive functions explained // Using recursion to sum an array of numbers
Java is a very powerful object oriented language. If you search for recursion in Wikipedia you will find this definition:
Recursion, in mathematics and computer science, is a method of defining functions in which the function being defined is applied within its own definition."
This means, that the function will call itself again and again until it gets the correct answer.
There are many pages where you can find great explanations to recursion theory. Most of them use the example of the Towers of Hanoi:
What I'm going to show you is an easy way to sum arrays of numbers. This example lets you see how easy can recursion be and how useful it is.
1public class Recursive {
2 public Recursive() {
3 /* We create the array of numbers we want to sum,
4 It can be any subclass of java.lang.Number */
5 Double[] test = {100d, 0.05d, 88d, 99d, 0.05d, 88d, 99d, 0.05d, 88d, 99d};
6 /* We call our function */
7 System.out.println(sum(test));
8 }
9 /* This is the initial function, it calculates the starting fields and results *automatically*/
10 public Number sum(Number[] numbers){
11 return sum(numbers[numbers.length - 1], numbers, numbers.length - 2);
12 }
13 /* This is our RECURSIVE FUNCTION */
14 public Number sum(Number initialValue, Number[] numbers, int location){
15 /* If we've reached the end of the array we return the final RESULT */
16 if(location < 0){
17 return initialValue;
18 }
19 /* Or else we are recursive */
20 else{
21 /*First we cast the java.lang.Number to its subclass so we can do the *sum */
22 if(numbers instanceof BigInteger[]){
23 return sum( ((BigInteger)initialValue).add((BigInteger)numbers[location]), numbers, (location - 1) );
24 } else if(numbers instanceof BigDecimal[]){
25 return sum( ((BigDecimal)initialValue).add((BigDecimal)numbers[location]), numbers, (location - 1) );
26 } else if(numbers instanceof Byte[]){
27 return sum( ((Byte)initialValue) + (Byte)numbers[location], numbers, (location - 1) );
28 } else if(numbers instanceof Double[]){
29 return sum( ((Double)initialValue) + ((Double)numbers[location]), numbers, (location - 1) );
30 } else if(numbers instanceof Integer[]){
31 return sum( ((Integer)initialValue)+ ((Integer)numbers[location]), numbers, (location - 1) );
32 } else if(numbers instanceof Long[]){
33 return sum( ((Long)initialValue)+ ((Long)numbers[location]), numbers, (location - 1) );
34 } else if(numbers instanceof Short[]){
35 return sum( ((Short)initialValue)+ ((Short)numbers[location]), numbers, (location - 1) );
36 } else{
37 return sum( ((Float)initialValue)+ ((Float)numbers[location]), numbers, (location - 1) );
38 }
39 }
40 }
41 public static void main(String[] args) {
42 new Recursive();
43 }
44}
Comments in "Java recursive functions explained // Using recursion to sum an array of numbers"
Functional programming languages like Haskell or Erlang do provide tail call optimization and in these languages a recursive call is just as expensive as an iteration.
You can read more about this issue at IBM: Diagnosing java code...