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

Single Parameter Lambda: Parentheses Optional

Learn when you can omit parentheses around a single parameter in lambda expressions.

Single Parameter Lambda: Parentheses Optional

You'll learn when you can simplify your lambda expressions by dropping the parentheses around a single parameter.

The Core Rule

When a lambda expression has exactly one parameter, Java lets you omit the parentheses around that parameter. This is a syntactic convenience that makes your code cleaner and more readable.

Think of it like dropping unnecessary punctuation in a sentence when the meaning is perfectly clear—you're not changing what it means, just making it easier to read.

The Syntax Options

For a single parameter, you have two equivalent ways to write the lambda:

With parentheses (always valid):

Predicate<String> hasLength = (s) -> s.length() > 0;

Without parentheses (cleaner):

Predicate<String> hasLength = s -> s.length() > 0;

Both are identical in behavior—Java understands them the same way.

When This Applies

This shortcut only works when:

  • You have exactly one parameter
  • You're using type inference (not declaring the parameter type explicitly)

If you have zero parameters or two or more parameters, you must use parentheses. If you explicitly declare the type, you also need parentheses.

Real-World Example

Consumer<String> printer = message -> System.out.println(message);

IntConsumer doubler = n -> System.out.println(n * 2);

Function<Integer, Integer> square = x -> x * x;

All of these are cleaner without the parentheses, and experienced Java developers prefer this style.

Key Takeaway: For single-parameter lambdas using type inference, you can drop the parentheses around the parameter to create cleaner, more readable code—write x -> x * 2 instead of (x) -> x * 2.