Lambda syntax in Java
(a, b) -> a + b, block bodies, type inference from the target.
Sticky-note instructions
A lambda is a compact, nameless function: **parameters, an arrow ->, and a body**. Think of a sticky note with instructions you can hand to someone else to follow later.
Function<Integer, Integer> twice = x -> x * 2;
twice.apply(21); // 42The four shapes
One untyped parameter needs no parentheses. Zero or several parameters need them. A single expression returns its value automatically; a **block body { } needs an explicit return**.
x -> x * 2 // one param
(a, b) -> a + b // two params
() -> 42 // none
(String s) -> { // typed, block
return s.length();
}A lambda needs a target
A lambda has no type of its own. It can only appear where a functional interface (exactly one abstract method) is expected. That target type tells the compiler the parameter types and the return type.
BinaryOperator<Integer> add = (a, b) -> a + b;
// target says: two Integers in, Integer out
Supplier<String> hi = () -> "hi";Your turn
What does this print?
BinaryOperator<Integer> mul = (a, b) -> a * b;
Function<String, Integer> f = s -> {
int n = s.length();
return n + 100;
};
System.out.println(mul.apply(4, 5));
System.out.println(f.apply("ok"));20 1029 10220 2
Show the answer
mul.apply(4, 5) is 20. The block lambda computes the length 2 and must return explicitly: 2 + 100 = 102.
No target, no lambda
What happens?
var twice = x -> x * 2;
System.out.println(twice);Prints a lambda descriptionCompile errorPrints 2
Show the answer
var takes its type from the right side, but a lambda gets its type from its target. Neither side knows the type, so the compiler can't tell which functional interface you mean: compile error.
The missing return
Add braces and the automatic return disappears. A block body for a method that returns a value **must say return**, or it won't compile.
Comparator<String> byLen = (a, b) -> {
Integer.compare(a.length(), b.length());
}; // ✗ missing return
Comparator<String> ok = (a, b) ->
Integer.compare(a.length(), b.length());Everywhere in modern Java
Sorting (list.sort((a, b) -> ...)), button click handlers, thread tasks (() -> doWork()), and every stream operation take lambdas. Before Java 8, each of these needed a bulky anonymous class of five or more lines.
Key takeaways
- x -> x * 2 (one parameter, no parentheses needed)
- (a, b) -> a + b and () -> 42
- Block body: (a, b) -> { int s = a + b; return s; }
- Parameter types are inferred from the target functional interface
💡 A lambda is like a recipe card handed to someone else: it describes what to do, and they decide when to cook.
The name comes from the lambda calculus, a model of computation invented by mathematician Alonzo Church in the 1930s, before electronic computers existed. Java gained lambdas in Java 8 (2014).
Practice questions
What does this print?
BinaryOperator<Integer> add = (a, b) -> a + b;
Function<String, Integer> len = s -> {
int n = s.length();
return n * 10;
};
System.out.println(add.apply(2, 3));
System.out.println(len.apply("hey"));- 5 30
- 5 3
- 23 30
- 5 hey10
Check your answer
5 30. add.apply(2, 3) returns 5. The block-bodied lambda computes the length 3 and explicitly returns 3 * 10 = 30.
What does this print?
var inc = x -> x + 1;
System.out.println(inc);- x -> x + 1
- Compile error
- 1
- Throws ClassCastException
Check your answer
Compile error. var needs a type from the right-hand side, but a lambda has no type without a target. The compiler can't tell which functional interface you mean.