What does the arrow operator (->) do in Java?
Codemia Knowledge Hub ·
The arrow operator (->) in Java separates the parameter list from the body in lambda expressions and switch expressions. It arrived with lambdas in Java 8 and was extended to switch in Java 14, and it is the core syntax behind functional-style Java.
-> in lambda expressions
A lambda expression is an anonymous function that implements a functional interface (an interface with exactly one abstract method). The parameters go on the left of the arrow and the implementation on the right:
The compiler infers parameter types from the target functional interface, so you rarely need to write them.
Replacing anonymous classes
Before Java 8, implementing a functional interface required an anonymous inner class. A lambda removes that boilerplate with identical behavior:
Lambdas in stream pipelines
The arrow operator is used heavily with the Streams API, where each lambda is invoked for every element:
Standard functional interfaces
java.util.function provides the interfaces lambdas usually target:
| Interface | Method Signature | Lambda Example |
Predicate<T> | boolean test(T t) | x -> x > 10 |
Function<T, R> | R apply(T t) | s -> s.length() |
Consumer<T> | void accept(T t) | s -> System.out.println(s) |
Supplier<T> | T get() | () -> new ArrayList<>() |
BiFunction<T, U, R> | R apply(T t, U u) | (a, b) -> a + b |
UnaryOperator<T> | T apply(T t) | x -> x * 2 |
BinaryOperator<T> | T apply(T t1, T t2) | (a, b) -> Math.max(a, b) |
You only need your own functional interface if none of these fit.
-> in switch expressions (Java 14+)
Java 14 added a second use: case VALUE -> branches in switch expressions replace the traditional case: ... break; pattern:
Arrow branches never fall through to the next case, which eliminates a whole class of bugs. When a branch needs several statements, use a block with yield:
Captured variables must be effectively final
Lambdas can use variables from the enclosing scope only if they are never reassigned after initialization:
The lambda may run later or on a different thread, and allowing mutation would introduce race conditions.
Method references (::) as shorthand
When a lambda just calls an existing method, a method reference can replace it:
Method references do not replace -> in general. They are sugar for the specific case where the body is a single method call that forwards all parameters directly.
Lambda mistakes to avoid
Returning a value in a void context. If the interface method returns void, an expression body that produces a value is allowed but can be confusing. Use a block body if the intent is unclear.
Missing return in a block body. An expression body (x -> x + 1) returns its result implicitly. A block body (x -> { x + 1; }) does not; write x -> { return x + 1; }.
Mixing up -> and ::. Use -> for custom logic and :: only when the body is a direct method delegation.
Long nested chains. Chains of more than 3 or 4 operations get hard to read. Extract complex lambdas into named methods and use method references.
Shadowing outer variables. A lambda parameter cannot share a name with a variable in the enclosing scope. Unlike anonymous classes, which create a new scope, this is a compilation error:
Related reading
- What does the keyword transient mean in Java?
- What does the question mark in Java generics' type parameter mean?
- What does the spring annotation ConditionalOnMissingBean do?
- What does the 'static' keyword do in a class?
- What does the static modifier after import mean?
- What does transitive true in Gradle exactly do w.r.t. crashlytics?
- What exactly is a Maven Snapshot and why do we need it?
- What exactly is Java EE?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.