0
0
JavaProgramBeginner · 2 min read

Java Program to Find Sum of Array Elements

You can find the sum of array elements in Java by looping through the array with a for loop and adding each element to a sum variable, like int sum = 0; for(int num : array) sum += num;.
📋

Examples

Input[1, 2, 3, 4, 5]
Output15
Input[10, -2, 7, 3]
Output18
Input[]
Output0
🧠

How to Think About It

To find the sum of array elements, think of adding each number one by one. Start with zero, then go through each element in the array and add it to your total. At the end, the total will be the sum of all elements.
📐

Algorithm

1
Initialize a variable sum to 0.
2
Loop through each element in the array.
3
Add the current element to sum.
4
After the loop ends, return or print the sum.
💻

Code

java
public class SumArray {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        int sum = 0;
        for (int num : numbers) {
            sum += num;
        }
        System.out.println("Sum of array elements: " + sum);
    }
}
Output
Sum of array elements: 15
🔍

Dry Run

Let's trace the array [1, 2, 3, 4, 5] through the code

1

Initialize sum

sum = 0

2

Add first element

sum = 0 + 1 = 1

3

Add second element

sum = 1 + 2 = 3

4

Add third element

sum = 3 + 3 = 6

5

Add fourth element

sum = 6 + 4 = 10

6

Add fifth element

sum = 10 + 5 = 15

7

Print result

Output: Sum of array elements: 15

IterationCurrent ElementSum After Addition
111
223
336
4410
5515
💡

Why This Works

Step 1: Initialize sum

We start with sum = 0 because adding zero does not change the total.

Step 2: Loop through array

The for loop goes through each element one by one to access all numbers.

Step 3: Add elements to sum

Each element is added to sum using sum += num, updating the total.

Step 4: Print the result

After the loop, the final sum holds the total of all elements and is printed.

🔄

Alternative Approaches

Using traditional for loop with index
java
public class SumArray {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        int sum = 0;
        for (int i = 0; i < numbers.length; i++) {
            sum += numbers[i];
        }
        System.out.println("Sum of array elements: " + sum);
    }
}
This uses an index to access elements, which is useful if you need the position of elements.
Using Java Streams (Java 8+)
java
import java.util.Arrays;
public class SumArray {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        int sum = Arrays.stream(numbers).sum();
        System.out.println("Sum of array elements: " + sum);
    }
}
This is a modern, concise way using streams but may be less clear for beginners.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops through each element once, so the time grows linearly with the array size, making it O(n).

Space Complexity

Only a few variables are used regardless of input size, so space complexity is O(1), constant space.

Which Approach is Fastest?

All approaches run in O(n) time; using streams may have slight overhead but is concise. The for-each loop is simple and efficient.

ApproachTimeSpaceBest For
For-each loopO(n)O(1)Simple and readable code
Traditional for loopO(n)O(1)When index needed
Java StreamsO(n)O(1)Concise modern code
💡
Use a for-each loop to keep your code simple and readable when summing array elements.
⚠️
Forgetting to initialize the sum variable to zero before adding elements causes wrong results.