An array is a type of Java object that is generated dynamically and acts as a container to store a fixed number of values of one kind. Arrays are used to store several items of the same kind in a single variable as opposed to establishing separate variables for each value. The length of an array is predetermined before it is built.
This tutorial will show you how to calculate the sum of an array’s elements in Java.
How Can Java Users Find the Sum of Array Elements?
The sum of an array’s elements is relatively possible because arrays are used to store data of a similar type. There are two ways to find the sum:
- With the use of loops
- With the use of the “Arrays.stream(input_array).sum()” method
Method 1: Finding the Sum of Array Elements with the Use of Loops
In this method, loops are used to find the sum of each element. Try the following example to understand this method:
class Sum_of_Array_elements {
public static void main(String[] args) {
int[] a = {123, 155, 170, 227,500};
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
System.out.println("The sum of each element of (a) is: " + sum);
}
}
In the above code:
- There are a class “Sum_of_Array_elements” that has a method “main()”.
- Inside this method, there is an array “a” of type “int” and a variable “sum” initialized with “0”.
- A “for loop” of the same length as the array “a” is used. Each element will be added to the “sum” by this loop till the loop is finished.
- Then the “sum” value is displayed on the screen.
Output
The following output will show the sum of each element of the array:

Method 2: Finding the Sum of Array Elements With the Use of the “Arrays.stream(input_array).sum()” Method
In this method the “Arrays.stream(input_array).sum()” method will automatically calculate the sum of each element so there is no need for any loop.
Syntax
Arrays.stream(input_array).sum();
Parameters
This method accepts the array whose sum we want as input. In the above syntax the “input_array” is the array whose sum will be calculated through this method.
Return type
This method returns the sun of each element whose type is similar to the type of the array.
Code
Try the following code to see how this method works:
import java.util.Arrays;
class Sum_of_Array_elements {
public static void main(String[] args) {
int[] a = {123, 155, 170, 227,500};
int sum = Arrays.stream(a).sum();
System.out.println("The sum of each element of (a) is: " + sum);
}
}
In this code,
- There is a library “java.util.Arrays” used at the start of the code.
- Then, a class named “Sum_of_Array_elements” is declared that has a “main()” method.
- After that an int array “a” is initialized.
- Then while declaring the variable “sum” the “Arrays.stream(input_array).sum()” Method is used.
- Finally the value of “sum” is printed.
Output

Because the same array values were used in both examples, we can see that the value of “sum” is identical to what it was in the previous method.
Finally, the ways to calculate the sum of an array’s elements are all covered in this article.
Conclusion
Arrays hold identical-type data. A collection of data may be stored in arrays, and the sum of each element can be determined. Loops are employed for this purpose. Additionally, the “Arrays.stream(input_array).sum()” Method in “Java” makes it easier to find the sum of each array element with fewer lines of code.