A logo showing the text blog.marcnuri.com
Español
Home»Java»Java recursive functions explained // Using recursion to sum an array of numbers

Recent Posts

  • Fabric8 Kubernetes Client 6.4.0 is now available!
  • I bought an iPad
  • Three years at Red Hat
  • Fabric8 Kubernetes Client 6.3.1 is now available!
  • Eclipse JKube 1.10 is now available!

Categories

  • Front-end
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • December 2015
  • November 2015
  • November 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

Java recursive functions explained // Using recursion to sum an array of numbers

2007-04-18 in Java / Legacy tagged Java / Programming / Recursion by Marc Nuri | Last updated: 2021-02-06

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:

  • Cut The Knot --Towers of Hanoi--
  • Recursion --Towers of Hanoi--
  • Java Recursion with examples

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}
Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Comments in "Java recursive functions explained // Using recursion to sum an array of numbers"

  • Avatar for Peter Veentjer
    Peter Veentjer
    2007-04-19 07:45
    The problem with recursive calls in Java is that Java doesn't do much 'tail call optimization'. The consequence is that for each call you need another stackframe, and this will lead to problems (stack overflow and performance).

    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.
  • Avatar for Marc Nuri
    Marc Nuri
    2007-04-19 08:13
    What you say is completely true. Although recursion is an elegant method for iteration, it can lead to stack overflow in many ocasions.

    You can read more about this issue at IBM: Diagnosing java code...

Post navigation

← Numbers to Strings with custom symbols // DecimalFormat - DecimalFormatSymbolsDisplaying a jTable inside another jTable // JTable cellRenderer →
© 2007 - 2023 Marc Nuri