Why it matters

Lambdas make Java APIs cleaner and callbacks less painful. Modern Java relies on them for streams, futures, event handlers, and configuration. Understanding lambdas is essential to writing idiomatic modern Java.

Advertisement

The architecture

Functional interface: an interface with exactly one abstract method. Runnable, Callable, Supplier, Function, Consumer, Predicate are all functional interfaces. Any single-abstract-method interface can be implemented with a lambda.

Lambda syntax: '(params) -> expression' or '(params) -> { statements; }'. Types can usually be inferred.

Java lambda mechanicsFunctional interfaceone abstract methodLambda expressionsugar for anon classMethod referenceClass::methodLambda compiles to invokedynamic + LambdaMetafactory for efficient dispatch
Three lambda forms.
Advertisement

How it works end to end

Method references: 'String::length', 'list::add' shorthand for lambdas that just call one method. Cleaner when the lambda body is a single method call.

Compilation: lambdas compile to invokedynamic bytecode that lazily generates lambda implementation at runtime via LambdaMetafactory. This is more efficient than anonymous class allocation.

Variable capture: lambdas can reference effectively-final outer variables. Modifying captured variables is a compile error.