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

Multiple Parameters: Parentheses Required

Master the syntax for lambda expressions that accept two or more parameters with required parentheses.

Multiple Parameters: Parentheses Required

You'll learn the precise syntax rule for lambda expressions that accept two or more parameters.

The Core Rule

When your lambda expression needs two or more parameters, you must wrap them in parentheses. This is non-negotiable syntax. While single-parameter lambdas give you the option to omit parentheses (as you learned previously), that flexibility disappears the moment you add a second parameter.

Think of it like a phone number: one digit doesn't need formatting, but multiple digits need structure (area code in parentheses, dashes, etc.). Similarly, Java needs parentheses to clearly separate multiple parameters.

The Syntax Pattern

// Two parameters - parentheses REQUIRED
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;

BiPredicate<String, Integer> isLongerThan = (text, length) -> text.length() > length;

BiConsumer<String, String> printPair = (first, second) -> 
    System.out.println(first + " and " + second);

Why This Rule Exists

Without parentheses, the compiler couldn't tell where one parameter ends and the next begins. Imagine writing a, b -> a + b — it creates ambiguity. The parentheses provide clear boundaries.

Common Mistake

// WRONG - compiler error
BiFunction<Integer, Integer, Integer> multiply = a, b -> a * b;

// CORRECT
BiFunction<Integer, Integer, Integer> multiply = (a, b) -> a * b;

The parentheses are part of the lambda's parameter declaration syntax, not optional style. Leave them out with multiple parameters, and your code won't compile.

Key Takeaway: Lambda expressions with two or more parameters must enclose the parameter list in parentheses: (param1, param2) -> expression.