• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to Find the Sum of Array Elements in Java?

July 31, 2023 //  by Talha Malik

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.

Category: Java

Previous Post: « Java 2D Array
Next Post: How to Truncate a String in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Optional.ofNullable() Method in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact