Integer sum() Method in Java

Last Updated : 21 Jan, 2026

The Integer.sum() method in Java is a static utility method provided by the java.lang.Integer class. It is used to return the sum of two integer values, behaving exactly like the + operator but offering better readability and usability in functional-style programming.

  • This method does not explicitly throw an exception at runtime.
  • However, if values exceed the int range (-2³¹ to 2³¹ - 1), integer overflow may occur.
  • If a literal value exceeds the int range, a compile-time error is thrown.

Example 1: This program demonstrates how to use the Integer.sum() method to add two integer values in Java.

Java
class GFG {
    public static void main(String[] args) {
        int a = 62;
        int b = 18;
        System.out.println("The sum is = " + Integer.sum(a, b));
    }
}

Explanation:

  • Integer.sum(a, b) adds 62 and 18.
  • The result 80 is returned and printed.

Syntax

public static int sum(int a, int b)

Parameter:

  • a -> the first integer value
  • b –> the second integer value 

Return Value: Returns an int value which is the sum of the two arguments.

How It Works

  • The method internally performs integer addition similar to: return a + b;
  • Being a static method, it can be called directly using the Integer class.

Example 2: Integer Overflow / Invalid Large Value

Java
class GFG {
    public static void main(String[] args) {
        int a = 92374612162; 
        int b = 181;
        System.out.println(Integer.sum(a, b));
    }
}

Output:

error: integer number too large

Explanation:

  • The value 92374612162 exceeds the int range.
  • Java throws a compile-time error before execution.

Example 3: Finding Sum of Integers Using a Loop

java
class GFG {
    public static void main(String[] args) {
        int[] arr = { 2, 4, 6, 8, 10 };
        int sum = 0;

        for (int i = 0; i < arr.length; i++) {
            sum = Integer.sum(sum, arr[i]);
        }
        System.out.println("Sum of array elements is: " + sum);
    }
}

Output
Sum of array elements is: 30

Explanation:

  • The Integer.sum() method is used inside a loop.
  • It adds each element to the running total.
Comment