Lambda Expressions
A lambda expression is an anonymous implementation of a functional interface. It lets Java treat a small piece of behavior as a value that can be passed to a method, stored in a variable, or composed with another operation. A lambda has no independent type: its target type comes from the context where it is used.

Syntax and target type
The basic form is (parameters) -> expression. Parentheses may be omitted for one inferred parameter. A block body uses braces and needs return when it produces a value.
Function<Integer, Integer> doubleValue = x -> x * 2;
BiFunction<Integer, Integer, Integer> add = (left, right) -> left + right;
Supplier<String> greeting = () -> "Hello";
Function<String, String> normalize = text -> {
String trimmed = text.trim();
return trimmed.toLowerCase();
};
The compiler infers parameter types from Function, BiFunction, or another target interface. You can write explicit types when clarity requires them: (String text) -> text.trim(). Do not mix inferred and explicit types in one parameter list.
| Form | Example | Use |
|---|---|---|
| one expression | x -> x * 2 |
Result is returned implicitly |
| several parameters | (a, b) -> a + b |
Use parentheses |
| no parameters | () -> clock.instant() |
Empty parameter list is required |
| block body | x -> { log(x); return x; } |
Several statements or local variables |
Captured variables
A lambda may read local variables only when they are final or effectively final. This restriction prevents a lambda from observing an uncontrolled mutable local variable after the enclosing method has moved on.
String prefix = "user:";
Function<Long, String> key = id -> prefix + id;
prefix is effectively final because it is assigned once. A lambda can mutate an object referenced by a captured variable, but that does not make the design safe for concurrent code. Prefer a lambda that depends on its parameters and has no hidden state.
Method references and readability
Use a lambda when it explains a small rule. Use a method reference when an existing method already expresses the rule: users.stream().map(User::name). Avoid deeply nested lambdas and side effects such as modifying a shared list inside forEach; they make order and failure behavior harder to see.
Glossary
- bytecode
- Platform-independent compiled code that the JVM can execute.
- class
- A blueprint that defines fields and methods for objects in object-oriented programming.
- object
- A runtime instance of a class with its own state and behavior.
- method
- A named block of behavior that belongs to a class or object and can be called by code.
- field
- A variable stored inside a class or object that represents part of its state.
- constructor
- A special method-like block used to create and initialize a new object.
- package
- A namespace that groups related Java classes and helps organize code.
- interface
- A contract that defines methods a class can implement without prescribing all internal details.
- generics
- A language feature that lets code work with types as parameters while preserving type safety.
- type erasure
- The Java compiler process that removes most generic type details from runtime bytecode.
- collection
- A data structure that stores a group of elements and provides operations over them.
- list
- An ordered collection where elements have positions and duplicates are allowed.
- set
- A collection that stores unique elements and is commonly used for membership checks.
- map
- A key-value data structure where each key points to a value.
- queue
- A collection designed for processing elements in a defined order, often first-in-first-out.
- lambda
- A compact function-like expression used to pass behavior as data.
- functional interface
- An interface with one abstract method, suitable for lambdas and method references.
- stream
- A Java API for processing data sequences with operations such as map, filter, and reduce.
- optional
- A container object that represents a value that may or may not be present.
- thread
- An independent path of execution inside a running program.
- synchronization
- Coordination that protects shared data when multiple threads access it.
- heap
- The runtime memory area where Java objects are allocated.
- stack
- The runtime memory area that stores method calls, local variables, and call frames.
- garbage collector
- A JVM component that automatically frees memory occupied by objects no longer reachable.
- memory leak
- A situation where memory remains occupied because objects are still referenced but no longer useful.