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 7.2 is now available!
  • Connecting to an MCP Server from JavaScript using AI SDK
  • Connecting to an MCP Server from JavaScript using LangChain.js
  • The Future of Developer Tools: Adapting to Machine-Based Developers
  • Connecting to a Model Context Protocol (MCP) Server from Java using LangChain4j

Categories

  • Artificial Intelligence
  • Front-end
  • Go
  • Industry and business
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • August 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • March 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
  • February 2020
  • January 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
  • January 2017
  • December 2015
  • November 2015
  • December 2014
  • March 2014
  • February 2011
  • November 2008
  • June 2008
  • May 2008
  • April 2008
  • January 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: 2023-10-27
Versión en Español

Recursion is one of the methods in computer science that can be used to solve a problem. Java is a very powerful object-oriented language which allows you to use recursion. 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 a function will call itself indefinitely. However, it is often done in a way that the function will call itself until it reaches a certain condition and stops. This condition is also called the base case.

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

In this post, I'm going to show you a simple example of recursion in Java. I'm going to use it to sum an array of numbers. The example illustrates how easy it is to use recursion and how useful it can be.

Recursion.java
import java.math.BigDecimal;

public class Recursion {

  public static void main(String[] args) {
    // We create the array of numbers we want to sum,
    // It can be any subclass of java.lang.Number
    Number[] test = {100, 0.05d, 1391L, 88d, 99d, 0.0037, 0.05d, 0.05d, 88d, 99d, -528.15, 0.13f};
    // We call our recursive function
    System.out.println(sum(test));
  }

  // This is the initial function
  // Calls the recursive function with the initial, default values
  static Number sum(Number[] numbers) {
    if (numbers.length < 1) {
      return 0;
    }
    return sum(BigDecimal.valueOf(0), numbers, numbers.length - 1);
  }

  // The recursive function
  static Number sum(BigDecimal initialValue, Number[] numbers, int location) {
    // If we've reached the end of the array we return the final result (base case)
    if (location < 0) {
      return initialValue;
    }
    // Otherwise, recursively call the function with the next number in the array
    else {
      int nextLocation = location - 1;
      Number current = numbers[location];
      // Convert the current number to BigDecimal
      // We can now add it to the initialValue with precision (or perform any arithmetic operation)
      return sum(initialValue.add(new BigDecimal(current.toString())), numbers, nextLocation);
    }
  }
}
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 - 2025 Marc Nuri