What are the syntactic forms a Java lambda expression can take? Show how the parameter list and body vary, and which parts can be omitted.
answer
- params -> body, arrow in the middle
- single inferred param: parens optional; zero params: parens required
- expression body = implicit return; block body = braces + explicit return
- types all-or-nothing: all inferred, all explicit, or all var
basics
~20 sA lambda is params -> body. Parameters go on the left, an arrow in the middle, the body on the right. With one parameter you can drop the parentheses; a single-expression body needs no braces or return, while a block body uses { } and an explicit return.
solid answer
~40 sA lambda has three parts: a parameter list, the `->` arrow, and a body. The parameter list can be empty `()`, a single name (parentheses optional, e.g. `x ->`), or several names in parentheses `(a, b) ->`. The body is either an expression — `x -> x * 2` — whose value is implicitly returned, or a block in braces — `x -> { ... return y; }` — which behaves like a method body and needs an explicit `return` for non-void results. Parameter types are usually inferred and omitted, but you may write them explicitly `(int x)` or use `var (var x)` — all-or-nothing across parameters. You cannot mix an inferred and an explicit type in the same list. A zero-arg lambda always needs the empty parentheses.
go deeper
Can write the four basic shapes and knows expression vs block body and where parentheses are optional.
Knows the all-or-nothing typing rule and that block bodies need an explicit return; can convert between an anonymous class and an equivalent lambda.
Explains when to prefer explicit/var parameter types (annotations, readability) and reads complex multi-line lambdas fluently.
Sets team conventions on lambda length and explicit typing, and recognizes when a lambda should be extracted into a named method or method reference for clarity.
## What a lambda is A **lambda expression** is a compact way to write an anonymous (unnamed) function — a block of behavior you can pass around as a value. Introduced in Java 8, it replaces a lot of verbose anonymous-class boilerplate. Syntactically every lambda is: ``` <parameter-list> -> <body> ``` The token `->` is the **arrow** that separates the inputs (left) from the work (right). ## The parameter list (left of the arrow) Three shapes: 1. **Zero parameters** — write empty parentheses: `() -> 42`. The parentheses are mandatory; you cannot omit them for a no-arg lambda. 2. **One parameter** — parentheses are *optional* when the type is inferred: `x -> x + 1` is identical to `(x) -> x + 1`. 3. **Many parameters** — parentheses are *required*, names comma-separated: `(a, b) -> a + b`. ## The body (right of the arrow) Two shapes: 1. **Expression body** — a single expression, no braces, no semicolon, no `return`. Its value is automatically the result: `x -> x * 2`. If the target method returns `void`, the expression is just evaluated for its side effect: `s -> System.out.println(s)`. 2. **Block body** — one or more statements in braces `{ }`. This behaves exactly like a regular method body: statements end in semicolons, and if the method returns a value you must write an explicit `return`. Example: ```java (a, b) -> { int sum = a + b; return sum; } ``` A common mistake is forgetting the `return` inside a block — the compiler then complains that the lambda has the wrong return type. ## Parameter types: inferred, explicit, or var Usually you omit parameter types and let the compiler infer them from context (see target typing): `(a, b) -> a + b`. You may instead write them **explicitly**: `(int a, int b) -> a + b`. Since Java 11 you may also use **`var`**: `(var a, var b) -> a + b` (useful when you want to attach an annotation to the parameter). The rule is **all-or-nothing**: every parameter must use the same style. You cannot write `(int a, b) ->` or `(var a, int b) ->`. With explicit or `var` types the parentheses are always required, even for a single parameter: `(int x) -> x`. ## Putting it together — the matrix | Params | Body | Example | |---|---|---| | none | expression | `() -> "hi"` | | one (inferred) | expression | `x -> x.trim()` | | one (explicit) | expression | `(String x) -> x.trim()` | | many | block | `(a, b) -> { return a + b; }` | Mastering these forms is purely mechanical, but it is the foundation for reading and writing all functional-style Java.
- When are the parentheses around a lambda's parameter list optional?Only for a single parameter whose type is inferred, e.g. `x -> x + 1`. With zero parameters, multiple parameters, or any explicit/`var` type, the parentheses are mandatory.
- Does an expression-body lambda need a `return`?No. The expression's value is implicitly returned. `return` (and braces) are only needed in a block body.
saying these in an interview costs you the question
- Thinking you can omit parentheses for a zero-arg or multi-arg lambda
- Forgetting the explicit `return` inside a block-body lambda
- Mixing inferred and explicit parameter types in one list (e.g. `(int a, b) ->`)
- Believing a single-expression body needs braces and a semicolon