Home Java Integer Methods sum() Java Integer sum() Method 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.
int a = Integer.sum(3, 4); // 7
int b = Integer.sum(-10, 4); // -6
int c = Integer.sum(Integer.MAX_VALUE, 1); // -2147483648 (wraps!) Call What 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.lang.Integer
public static int sum(int a, int b) Parameters Parameter Description a (int)The first number. b (int)The second number.
Return Value Return Value Description intThe sum of a and b. If it does not fit, it wraps around between Integer.MAX_VALUE and Integer.MIN_VALUE.
Important
This method was added in Java 8, and its main purpose is to be used as Integer::sum. It does not check for overflow: Integer.sum(Integer.MAX_VALUE, 1) is Integer.MIN_VALUE, with no warning. If large totals are possible, use Math.addExact to fail loudly, or add into a long instead.
Compare with Similar Methods Related methods:
Method How 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 1. Same as + 2. Total of a list 3. Silent overflow Example 1 — Add Two Numbers with Integer.sum Call sum on a few pairs and check it against the + operator.
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));
}
} sum(3, 4): 7
sum(-10, 4): -6
sum(0, 0): 0
same as +: true How It Works Plain addition. 3 plus 4 is 7, and -10 plus 4 is -6.Zero is neutral. Adding 0 and 0 gives 0.Identical to the operator. The last line proves that Integer.sum(250, 750) and 250 + 750 are the same number, 1000.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.
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);
}
} array total: 480
items: 12
empty total: 0 How It Works reduce with a starting value. The first argument, 0, is where the running total begins.One pair at a time. Integer::sum adds the running total and the next price: 120 + 45, then + 300, then + 15, which is 480.Works on lists too. A list of boxed Integer values adds up the same way, and 2 + 5 + 1 + 4 is 12.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.
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");
}
}
} MAX + 1 with sum: -2147483648
MAX + MAX: -2
as long instead: 2147483648
addExact -> ArithmeticException How It Works It wraps around. One more than MAX_VALUE does not fit, so the result jumps to MIN_VALUE, -2147483648.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.A long has room. Casting one operand to long first makes the addition happen in 64 bits, so the answer is the true 2147483648.addExact complains. Math.addExact detects the overflow and throws ArithmeticException instead of returning a wrong number.Frequently Asked Questions What does Integer.sum do in Java? 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.
How do I add all numbers in a list with Integer::sum? Use list.stream().reduce(0, Integer::sum), or for an int array Arrays.stream(array).reduce(0, Integer::sum).
Does Integer.sum check for overflow? No. It wraps around silently. Use Math.addExact to get an ArithmeticException, or add into a long.
Is Integer.sum faster than the + operator? No. They compile to the same operation. Use sum only when you need a method reference.
Which Java version added Integer.sum? Java 8, together with Integer.max and Integer.min.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion