Lambda Expressions in Java
A lambda expression is a compact, unnamed block of code that can be passed around as a value — (a, b) -> a + b defines a small function taking two parameters and returning their sum, without needing to write a full named method or a class to hold one. Lambdas exist specifically to implement functional interfaces, interfaces with exactly one abstract method, providing just that one method's body inline, right where it's needed, most commonly as an argument to another method.
The syntax scales with how much the lambda needs to do. A single expression like a -> a * 2 can skip both the braces and an explicit return, since the expression's value is returned automatically, while a lambda with multiple statements needs braces and an explicit return just like a regular method body would.
The basic syntax
(parameters) -> expression defines a small, unnamed function in one line.
What a lambda is for
Implementing a functional interface's single abstract method inline, without a separate named class or method.
Single-expression form
a -> a * 2 needs no braces or return keyword; the expression's value becomes the result automatically.
Multi-statement form
A lambda with more than one statement needs braces and an explicit return, exactly like an ordinary method body.
Passed around like any other value
A lambda can be stored in a variable, passed as an argument, or returned from a method, wherever the matching functional interface type is expected.
import java.util.function.BinaryOperator; public class LambdaDemo { public static void main(String[] args) { BinaryOperator<Integer> add = (a, b) -> a + b; System.out.println("Sum: " + add.apply(3, 4)); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.