Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Keywords and Statements

Java lambda expressions

Reference for Java lambda expressions: syntax forms, functional interfaces, target typing, variable capture, this inside lambdas and common mistakes.

A lambda expression is a compact way to write an anonymous function: a list of parameters, an arrow ->, and a body. Lambdas were added in Java 8. They can be used anywhere the compiler expects a functional interface, meaning an interface with exactly one abstract method, such as Runnable, Comparator<T>, or Function<T, R>.

A lambda has no type of its own. Its type is taken from the context (the target type), and the lambda body becomes the implementation of that single abstract method.

Java
Comparator<String> byLength = (a, b) -> Integer.compare(a.length(), b.length());
Runnable r = () -> System.out.println("run");
Function<Integer, Integer> twice = x -> x * 2;

Try it

Java Lambda forms

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Capturing variables and this

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
(parameters) -> expression
(parameters) -> { statements; }
Form Example
No parameters () -> 42
One parameter, inferred type x -> x + 1 or (x) -> x + 1
Several parameters (a, b) -> a + b
Explicit types (int a, int b) -> a + b (all or none)
var parameters (var a, var b) -> a + b (all must use var)
Block body x -> { log(x); return x * 2; } (needs return for a value)
Void-compatible body x -> System.out.println(x) (the expression value is discarded)

Usage notes

  • Functional interface required: the target type must have exactly one abstract method (methods inherited from Object such as equals don't count; default and static methods don't count). @FunctionalInterface asks the compiler to verify this.
  • Capture rule: a lambda can use local variables and parameters of the enclosing method only if they are final or effectively final (never reassigned). Fields and array elements have no such limit, but mutating shared state from several threads needs synchronization.
  • this and scoping: inside a lambda this means the enclosing instance, and a lambda parameter cannot reuse the name of a local variable in scope. An anonymous class creates a new scope with its own this.
  • Checked exceptions: a lambda body may throw a checked exception only if the functional interface's method declares it. Callable.call() does, Runnable.run() and Function.apply() do not.
  • Not a class: lambdas are compiled to invokedynamic call sites, not to inner class files. Their getClass().getName() is generated and not stable, and they do not override toString(), equals(), or hashCode() in any useful way.
  • Overload ambiguity: passing a lambda to overloaded methods (for example submit(Runnable) versus submit(Callable)) can be ambiguous; a lambda that returns a value picks Callable.
  • Readability: long block lambdas are better extracted into a named method and passed as a method reference.
  • Recursion needs a field or a helper, since a lambda cannot refer to the variable it is being assigned to when that is local.
  • Serializable lambdas exist (cast to an intersection type) but are fragile; avoid them.