λ Lambdas & Functional Java · Intermediate

Lambda syntax in Java

(a, b) -> a + b, block bodies, type inference from the target.

🧩 The mysteryWhat if you could hand a method a tiny sticky note that says "double whatever you get", and it just... works? That's a lambda: behaviour you can pass around like a value.

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);   // 42

The 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";
🔮 Predict it

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"));
  1. 20 102
  2. 9 102
  3. 20 2
Show the answer

mul.apply(4, 5) is 20. The block lambda computes the length 2 and must return explicitly: 2 + 100 = 102.

🔮 Predict it

No target, no lambda

What happens?

var twice = x -> x * 2;
System.out.println(twice);
  1. Prints a lambda description
  2. Compile error
  3. Prints 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 trap

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());
💼 In the real world

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

  1. x -> x * 2 (one parameter, no parentheses needed)
  2. (a, b) -> a + b and () -> 42
  3. Block body: (a, b) -> { int s = a + b; return s; }
  4. 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.

🤯 Did you know?

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"));
  1. 5 30
  2. 5 3
  3. 23 30
  4. 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);
  1. x -> x + 1
  2. Compile error
  3. 1
  4. 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.

Next: Function, Predicate, Consumer, Supplier. Java ships ready-made lambda shapes. Can you tell them apart at a glance?