beginnerFree
Lambda Expressions
Concise syntax for anonymous functions

Lambdas replace verbose anonymous classes with arrow syntax: (params) -> body.
Watch
The narrated version of this skribble — tap play for the full-size video.
0:00 / 1:13
Deep dive
The Big Picture
Lambdas are anonymous functions - concise syntax for implementing functional interfaces.
- Syntax:
(params) -> expression - Or block form:
(params) -> { body; } - Essential for streams:
list.forEach(x -> print(x))
Replaced bulky anonymous classes for single-method callbacks.
How It Works
Lambdas capture variables from enclosing scope (closure). Captured variables must be effectively final.
invokedynamiccall site; runtime bootstrap links the lambda impl- Non-capturing lambdas are often cached (no per-call allocation)
- Capturing lambdas create lightweight objects
- Compiler infers types from context (target typing)
In the Java Landscape
Java 8's flagship feature alongside streams.
- Work with Stream API:
map,filter,reduce - Replace
Comparatorboilerplate:Comparator.comparingInt(p -> p.age) - Method references shorthand:
System.out::println - Scala, Groovy, C# had closures before Java
- Modern libraries designed around lambda parameters
Pitfalls and Best Practices
- Can't reassign captured locals - must be
effectively final - Type inference can fail - add explicit types
- Debugging harder - stack traces show synthetic names
- Prefer expression lambdas (
x -> x * 2) over blocks - Don't return from lambda to exit enclosing method



