Start Coding Now
📊 Array Programsbeginner1 methods

Sum of Array Elements in Java

Java program to calculate the sum of all elements in an array.

Last updated: 11 January 2026

Method 1: Using For Loop

Iterate and add elements.

public class ArraySum {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int sum = 0;
        
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        
        System.out.println("Sum of all elements: " + sum);
    }
}
Output:
Sum of all elements: 15

Explanation

We initialize sum=0 and iterate through the array adding each element to sum.

Frequently Asked Questions

Try This Program

Copy this code and run it in our free online Java compiler.

Open Java Compiler