HOWTO · Java
How to Get the Sum of an Array in Java
Learn how to find the sum of an array in Java using loops and streams.
On this page
To total an int[] in Java, use Arrays.stream(numbers).sum() for a concise built-in approach, or add each value with a for loop when you want explicit control. Java arrays do not have an instance sum() method; Arrays.stream(int[]) turns the values into an IntStream, which provides sum() and reduce().
Summing an array is one of the core data operations in the Java Tutorial, which orders the array and collection guides from basics to streams.
The examples below use the same six values, whose arithmetic total is 149. Each program is self-contained and can be saved as SimpleTesting.java.
Sum an int array with a for loop
A regular loop gives each step a visible place: set the accumulator, visit every index, and add the corresponding element. It is a straightforward choice when you want to follow the calculation or add other logic during traversal.
This program adds the six values in numbers and prints their total. The loop condition uses numbers.length, and the array name is used consistently for its length and element access.
public class SimpleTesting {
public static void main(String[] args) {
int[] numbers = {12, 34, 45, 21, 33, 4};
int total = 0;
for (int i = 0; i < numbers.length; i++) {
total += numbers[i];
}
System.out.println("Array Sum = " + total);
}
}
Array Sum = 149
Sum an int array with a parallel stream and reduce
Call parallel() on the IntStream returned by Arrays.stream(numbers) to use a parallel stream. The two-argument reduce takes an identity value (0) and an addition operation. Addition is associative, so partial totals can be combined to produce the same result. For a short array, prefer the sequential version unless parallel processing suits the surrounding workload; parallel execution does not automatically make a small calculation faster.
import java.util.Arrays;
public class SimpleTesting {
public static void main(String[] args) {
int[] numbers = {12, 34, 45, 21, 33, 4};
int total = Arrays.stream(numbers)
.parallel()
.reduce(0, (left, right) -> left + right);
System.out.println("Array Sum = " + total);
}
}
Array Sum = 149
Sum an int array with Arrays.stream(...).reduce(...)
For a sequential stream, omit parallel(). Arrays.stream(int[]) still returns an IntStream; reduce(0, Integer::sum) starts with the identity value and combines each primitive value through addition. The identity keeps the operation well-defined when there are no values to combine.
import java.util.Arrays;
public class SimpleTesting {
public static void main(String[] args) {
int[] numbers = {12, 34, 45, 21, 33, 4};
int total = Arrays.stream(numbers).reduce(0, Integer::sum);
System.out.println("Array Sum = " + total);
}
}
Array Sum = 149
Sum an int array with Arrays.stream(...).sum()
When the operation is addition, sum() states the intent more directly than a reduction. It is a terminal operation on the IntStream produced from the array, and returns an int total.
Because this method returns int, the total uses Java’s 32-bit integer arithmetic. If the mathematical total may exceed the int range, widen the stream before adding, for example with Arrays.stream(numbers).asLongStream().sum(). The ordinary loop also needs a long accumulator and a widened value when handling that range.
import java.util.Arrays;
public class SimpleTesting {
public static void main(String[] args) {
int[] numbers = {12, 34, 45, 21, 33, 4};
int total = Arrays.stream(numbers).sum();
System.out.println("Array Sum = " + total);
}
}
Array Sum = 149
Sum an int array with IntStream.of(...).sum()
IntStream.of(numbers) is another direct way to create an IntStream from an int[]. Its sum() operation returns the same total as Arrays.stream(numbers).sum(). Choose either form based on which reads more naturally in your code.
import java.util.stream.IntStream;
public class SimpleTesting {
public static void main(String[] args) {
int[] numbers = {12, 34, 45, 21, 33, 4};
int total = IntStream.of(numbers).sum();
System.out.println("Array Sum = " + total);
}
}
Array Sum = 149
Sum an int array with a compact for loop
The loop below puts initialization, continuation, and accumulation into the for header, then visits the array from its last index to its first. It is valid Java, but the empty body and decrement inside the update expression make it harder to scan than the ordinary loop. Use the clearer form unless compactness is specifically useful.
public class SimpleTesting {
public static void main(String[] args) {
int[] numbers = {12, 34, 45, 21, 33, 4};
int total;
int i;
for (total = 0, i = numbers.length - 1;
i >= 0;
total += numbers[i--]) {
// Accumulation happens in the loop update expression.
}
System.out.println("Array Sum = " + total);
}
}
Array Sum = 149