Learn Java
import com.learnjava.methods.LambdaExpressions;

Lambda Expressions in Java

@Concept

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.

@HowItWorks
1

The basic syntax

(parameters) -> expression defines a small, unnamed function in one line.

2

What a lambda is for

Implementing a functional interface's single abstract method inline, without a separate named class or method.

3

Single-expression form

a -> a * 2 needs no braces or return keyword; the expression's value becomes the result automatically.

4

Multi-statement form

A lambda with more than one statement needs braces and an explicit return, exactly like an ordinary method body.

5

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.

@Example
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));
}
}
Output
Sum: 7
1add is declared as a BinaryOperator<Integer>, a functional interface expecting two Integer inputs and one Integer output.
2The lambda (a, b) -> a + b supplies that interface's single abstract method body directly, with no named class needed.
3add.apply(3, 4) actually invokes the lambda's logic, computing 3 + 4.
4The single println call prints that computed result, 7, as one line.
@UseCases
Passing a small piece of custom logic directly into a method, like a comparison rule for sorting
Writing a short callback or event handler without a separate named class
Replacing a one-method anonymous class with a shorter, equivalent lambda
Defining reusable small operations, like a mathematical transformation, as a stored value

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.