Lambda Expression Structure: Arrow Notation
You'll learn how the arrow (->) operator connects parameters to behavior in lambda expressions.
The Arrow: Java's "Becomes" Symbol
At its core, a lambda expression is a compact way to represent behavior without the ceremony of a full method declaration. The arrow operator (->) is the bridge that separates what goes in (parameters) from what happens (the body).
Think of the arrow as saying "these inputs become this result" or "given these values, do this action."
The Basic Anatomy
Every lambda expression follows this pattern:
(parameters) -> expression_or_block
Left side of arrow: Parameter list—what the lambda receives
The arrow itself: -> operator
Right side of arrow: The body—what the lambda does or returns
Here are concrete examples you've already seen:
// Predicate with one parameter
x -> x > 10
// BiFunction with two parameters
(a, b) -> a + b
// Consumer with no return
str -> System.out.println(str)
// Supplier with no parameters
() -> "Default Value"
// Block body with braces
(x, y) -> {
int sum = x + y;
return sum * 2;
}
The arrow visually separates the input contract from the implementation. When you see (String s) -> s.length(), you immediately know: "given a String, produce its length."
Key Takeaway: The arrow operator (->) is the signature element of lambda syntax, cleanly dividing parameters on the left from the body on the right, creating a visual flow from input to action.