Lambda: An expression-body lambda and a block-body lambda are equivalent

A lambda can be written as a single expression whose value is returned implicitly, or as a braced block with an explicit return statement and explicit parameter types; both forms produce the same functional interface implementation.

Code
BinaryOperator<Integer> addExpr = (a, b) -> a + b;
BinaryOperator<Integer> addBlock = (Integer a, Integer b) -> {
    int sum = a + b;
    return sum;
};
System.out.println("Expression body: " + addExpr.apply(2, 3));
System.out.println("Block body: " + addBlock.apply(2, 3));
Output
Expression body: 5
Block body: 5
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms