VOOZH about

URL: https://www.geeksforgeeks.org/java/integer-sum-method-in-java/

⇱ Integer sum() Method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.

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

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


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.

Related Topics

Comment