Java Integer sum() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

In Java, Integer.sum(a, b) returns a + b for two int values. It does exactly what the + operator does, but as a method.

That makes it possible to write Integer::sum as a method reference, for example to add up all the numbers in a stream with reduce. For a single addition, the plain + is shorter.

A static method of java.lang.Integer (since Java 8): call it on the class, as Integer.sum(...). Integer lives in java.lang, so no import is needed. Integer.sum() was added in Java 8.

Syntax

Pass the two numbers to add.

Java
int a = Integer.sum(3, 4);                // 7
int b = Integer.sum(-10, 4);               // -6
int c = Integer.sum(Integer.MAX_VALUE, 1); // -2147483648 (wraps!)
CallWhat it does
Integer.sum(a, b)The same as a + b for int values.
stream.reduce(0, Integer::sum)Adds every number in a stream, starting from 0.
Math.addExact(a, b)Adds, but throws ArithmeticException on overflow.
(long) a + bAdds in long arithmetic so large values do not wrap.

Integer.sum wraps around silently when the true result does not fit in an int, exactly like +.

Library declarations
Java
// java.lang.Integer
public static int sum(int a, int b)

Parameters

ParameterDescription
a (int)The first number.
b (int)The second number.

Return Value

Return ValueDescription
intThe sum of a and b. If it does not fit, it wraps around between Integer.MAX_VALUE and Integer.MIN_VALUE.

Compare with Similar Methods

Related methods:

MethodHow it differs
max(a, b)Another Java 8 method that works well as a method reference.
min(a, b)The same idea for the smaller value.
toUnsignedLong(i)Widens to long, useful before adding large unsigned values.
Collections.addAll()Adds elements to a collection, not numbers together.

Java Examples

Example 1 — Add Two Numbers with Integer.sum

Call sum on a few pairs and check it against the + operator.

Java
public class IntegerSumDemo {
    public static void main(String[] args) {
        System.out.println("sum(3, 4):      " + Integer.sum(3, 4));
        System.out.println("sum(-10, 4):    " + Integer.sum(-10, 4));
        System.out.println("sum(0, 0):      " + Integer.sum(0, 0));
        System.out.println("same as +:      " + (Integer.sum(250, 750) == 250 + 750));
    }
}

How It Works

  1. Plain addition. 3 plus 4 is 7, and -10 plus 4 is -6.
  2. Zero is neutral. Adding 0 and 0 gives 0.
  3. Identical to the operator. The last line proves that Integer.sum(250, 750) and 250 + 750 are the same number, 1000.
  4. Why it exists. Having addition as a method lets you hand it to other code as Integer::sum.

Example 2 — Total a Shopping Cart with Integer::sum

Use Integer::sum to add up the prices in an array and in a list.

Java
import java.util.Arrays;
import java.util.List;

public class IntegerSumReduce {
    public static void main(String[] args) {
        int[] prices = {120, 45, 300, 15};
        int total = Arrays.stream(prices).reduce(0, Integer::sum);
        System.out.println("array total: " + total);

        List<Integer> quantities = Arrays.asList(2, 5, 1, 4);
        int items = quantities.stream().reduce(0, Integer::sum);
        System.out.println("items:       " + items);

        int empty = Arrays.stream(new int[0]).reduce(0, Integer::sum);
        System.out.println("empty total: " + empty);
    }
}

How It Works

  1. reduce with a starting value. The first argument, 0, is where the running total begins.
  2. One pair at a time. Integer::sum adds the running total and the next price: 120 + 45, then + 300, then + 15, which is 480.
  3. Works on lists too. A list of boxed Integer values adds up the same way, and 2 + 5 + 1 + 4 is 12.
  4. The start matters for empty input. With nothing to add, reduce simply returns its starting value, 0.

Example 3 — See Overflow Wrap Around and Catch It

Add numbers that do not fit, then compare with Math.addExact and with long arithmetic.

Java
public class IntegerSumOverflow {
    public static void main(String[] args) {
        int big = Integer.MAX_VALUE;

        System.out.println("MAX + 1 with sum:  " + Integer.sum(big, 1));
        System.out.println("MAX + MAX:         " + Integer.sum(big, big));
        System.out.println("as long instead:   " + ((long) big + 1));

        try {
            Math.addExact(big, 1);
        } catch (ArithmeticException e) {
            System.out.println("addExact -> ArithmeticException");
        }
    }
}

How It Works

  1. It wraps around. One more than MAX_VALUE does not fit, so the result jumps to MIN_VALUE, -2147483648.
  2. Doubling the maximum. MAX_VALUE + MAX_VALUE wraps to -2, because the true total is 4294967294 and the int keeps only the low 32 bits.
  3. A long has room. Casting one operand to long first makes the addition happen in 64 bits, so the answer is the true 2147483648.
  4. addExact complains. Math.addExact detects the overflow and throws ArithmeticException instead of returning a wrong number.

Frequently Asked Questions

It adds two int values and returns the result, exactly like a + b. It exists so you can use it as the method reference Integer::sum.
Use list.stream().reduce(0, Integer::sum), or for an int array Arrays.stream(array).reduce(0, Integer::sum).
No. It wraps around silently. Use Math.addExact to get an ArithmeticException, or add into a long.
No. They compile to the same operation. Use sum only when you need a method reference.
Java 8, together with Integer.max and Integer.min.

Next: toBinaryString()

Show the bits of an int as a binary String.

toBinaryString() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful