Course contentsShow
← Java
Lesson 1703 of 2,87036. Lambda Expressions and Method ReferencesFree lesson

Expression Body: Single Statement Lambdas

Create concise lambdas with expression bodies that implicitly return a value without braces or return keyword.

Expression Body: Single Statement Lambdas

You'll learn how to write ultra-compact lambdas that return values without curly braces or explicit return statements.

The Core Idea

When your lambda contains just a single expression, Java lets you skip the braces {} and the return keyword entirely. The expression's result is automatically returned. This is called an expression body lambda, and it's the most concise form you can write.

Think of it like a mathematical function: f(x) = x * 2 doesn't need extra syntax to say "return this value"—the expression itself is the result.

From Block Body to Expression Body

Here's the transformation:

// Block body (explicit return)
Function<Integer, Integer> doubler = (x) -> { return x * 2; };

// Expression body (implicit return)
Function<Integer, Integer> doubler = (x) -> x * 2;

Both do the exact same thing, but the second version is cleaner and more readable.

When You Can Use Expression Bodies

Use an expression body when:

  • Your lambda has exactly one statement
  • That statement produces a value (for functions that return something)
Predicate<String> isEmpty = s -> s.isEmpty();
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
Consumer<String> printer = msg -> System.out.println(msg);
Supplier<Double> random = () -> Math.random();

Notice how clean these are—no clutter, just the essential logic.

What Happens Behind the Scenes

The Java compiler automatically treats the expression's value as the return value. You cannot mix expression bodies with explicit return keywords—that would require braces.

Key Takeaway: Expression body lambdas omit braces and return for single-statement logic, making your code more concise and readable by implicitly returning the expression's result.