Java Program to Find Sum of Array Elements

Last Updated : 23 Jan, 2026

Given an array of integers, the task is to find the sum of its elements by traversing the array and adding each value. In Java, this can be achieved using multiple approaches such as iterative loops, the Stream API, or recursion, each offering different trade-offs in terms of readability, performance, and space usage.

Examples:

Input : arr[] = {1, 2, 3}
Output: 6
1 + 2 + 3 = 6

Input : arr[] = {15, 12, 13, 10}
Output: 50
15 + 12 + 13 + 10 = 50

Approach 1: Iterative Method (Using Loop)

Algorithm

  • Initialize a variable sum to 0.
  • Traverse the array using a loop.
  • Add each element to sum.
  • Print the value of sum.
Java
class Test {
    static int arr[] = {12, 3, 4, 15};
    static int sum() {
        int sum = 0;
        for (int i = 0; i < arr.length; i++)
            sum += arr[i];

        return sum;
    }
    public static void main(String[] args) {
        System.out.println("Sum of given array is " + sum());
    }
}

Output
Sum of given array is 34

Explanation:

  • The method sum() is defined to calculate the sum of all array elements.
  • Inside sum(), a variable sum is initialized to 0.
  • A for loop iterates through each element of the array.
  • Each element is added to the sum variable.
  • Time Complexity: O(n) and Auxiliary Space: O(1)

Approach 2: One-Line Solution Using Java Streams

Java provides an inbuilt method to compute the sum using the Stream API.

Syntax

Arrays.stream(arrayName).sum();

Java
import java.util.Arrays;
class GFG {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5};
        int sum = Arrays.stream(nums).sum();
        System.out.println(sum);
    }
}

Output
15

Explanation:

  • An integer array nums is created with values {1, 2, 3, 4, 5}.
  • Arrays.stream(nums) converts the array into a stream of integers.
  • The sum() method of the stream calculates the total of all elements.
  • The resulting sum is stored in the variable sum.
  • Time Complexity: O(n) and Auxiliary Space: O(1)

Approach 3: Using Recursion

In this approach, the sum is calculated by recursively adding array elements.

Java
class GFG {
    static int sumArray(int[] arr, int n) {
        if (n == 0)
            return arr[0];
        return arr[n] + sumArray(arr, n - 1);
    }
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int sum = sumArray(arr, arr.length - 1);
        System.out.println("Sum of the elements in the array: " + sum);
    }
}

Output
Sum of the elements in the array: 15

Explanation:

  • An integer array arr is created with values {1, 2, 3, 4, 5}.
  • The recursive method sumArray(int[] arr, int n) calculates the sum:
  • Base case: If n == 0, return the first element arr[0].
  • Recursive step: Return arr[n] + sumArray(arr, n - 1) to add the current element to the sum of previous elements.
  • Time Complexity: O(n) and Auxiliary Space: O(n) (recursive call stack).
Comment