CKTIS / Java
Integer.sum — adding two int values
`Integer.sum(a, b)` adds two `int` values just like `+`. Use `Integer::sum` when an API needs you to pass addition as a function.
Version: Java SE 21
Syntax
public static int sum(int a, int b)
Integer.sum(a, b)
Integer::sumInteger.sum is a static method of java.lang.Integer, available since Java 8. It takes exactly two primitive int values and returns an int. You may see Integer.sum() used as shorthand in prose, but it does not compile as a call: there is no zero-argument overload. Call Integer.sum(a, b) instead.
For two int values, the result is the same as a + b. On overflow, the result wraps within the 32-bit int range without throwing an exception: Integer.sum(Integer.MAX_VALUE, 1) returns Integer.MIN_VALUE. Use Math.addExact(a, b) if overflow should throw ArithmeticException. An Integer argument is automatically unboxed; unboxing null throws NullPointerException.
Integer::sum is a method reference, not a call. Its target type, such as IntBinaryOperator, determines how it can be used. Calling add.applyAsInt(20, 22) performs the addition and returns 42. The method itself changes no state and does not update an AtomicInteger.
In a Map<String, Integer>, merge(key, value, Integer::sum) stores the sum of the existing non-null value and the incoming value. If the key is absent or maps to null, it stores the incoming value without calling the function; the incoming value must not be null. On a Stream<Integer>, reduce(0, Integer::sum, Integer::sum) uses 0 as the identity: the first reference adds the next element to the running result, and the second combines partial results.
Examples
import java.util.HashMap;
import java.util.Map;
import java.util.function.IntBinaryOperator;
import java.util.stream.Stream;
public class IntegerSumDemo {
public static void main(String[] args) {
System.out.println(Integer.sum(20, 22));
IntBinaryOperator add = Integer::sum;
System.out.println(add.applyAsInt(20, 22));
Map<String, Integer> totals = new HashMap<>();
totals.put("points", 20);
System.out.println(totals.merge("points", 22, Integer::sum));
int total = Stream.of(10, 12, 20)
.reduce(0, Integer::sum, Integer::sum);
System.out.println(total);
System.out.println(Integer.sum(Integer.MAX_VALUE, 1));
}
}Result
42 42 42 42 -2147483648
The first four lines show a direct call, a method reference, a map merge, and a stream reduction, in that order. The last line shows overflow.
Related lessons
- Java · Lesson 41 · Advanced · Version: 2
- Java · Lesson 56 · Basic · Version: 2
- Java · Lesson 65 · Basic · Version: 2
- Java · Lesson 66 · Basic · Version: 2
- Java · Lesson 39 · Intermediate · Version: 2
- Java · Lesson 56 · Intermediate · Version: 2
- Java · Lesson 57 · Intermediate · Version: 2
Sources
- Java SE 21 API — Integer.sum(int, int)
- Java Language Specification, Java SE 21 — §15.13 Method Reference Expressions
- Java Language Specification, Java SE 21 — §15.18.2 Additive Operators (+ and -) for Numeric Types
- Java Language Specification, Java SE 21 — §5.1.8 Unboxing Conversion
- Java SE 21 API — Math.addExact(int, int)
- Java SE 21 API — IntBinaryOperator
- Java SE 21 API — Map.merge
- Java SE 21 API — Stream.reduce with identity, accumulator, and combiner