What is a lambda expression in Java?
Learn what a lambda expression is in Java, how it implements functional interfaces, variable capture rules, and how it replaces anonymous inner classes.
Expected Interview Answer
A lambda expression in Java is a concise, anonymous block of code that implements a functional interface (an interface with exactly one abstract method), letting you pass behavior as a value without writing a full named class.
Introduced in Java 8, the syntax (parameters) -> expression or (parameters) -> { statements } compiles to an instance of whatever functional interface the context expects, such as Runnable, Comparator<T>, or a custom interface like Predicate<T>. Lambdas capture surrounding local variables by value, and those variables must be effectively final. They dramatically shorten code that previously required verbose anonymous inner classes, and they underpin the Stream API, event listeners, and Comparator-based sorting throughout modern Java.
- Removes boilerplate compared to anonymous inner classes
- Enables passing behavior as arguments to methods
- Powers the Stream API's map, filter, and reduce operations
- Works with any functional interface, built-in or custom
- Improves readability for short, single-purpose logic
AI Mentor Explanation
A lambda expression is like a quick hand signal a captain gives a fielder rather than walking over and explaining a whole fielding plan in a huddle. The signal only works because there's an agreed single meaning behind it — one specific instruction, nothing more — the same way a lambda only fits an interface with one abstract method.
Step-by-Step Explanation
Step 1
Identify a functional interface
Find or define an interface with exactly one abstract method, like Runnable, Comparator<T>, or a custom @FunctionalInterface.
Step 2
Write the lambda syntax
Use (parameters) -> expression for a single expression, or (parameters) -> { statements; return value; } for a block body.
Step 3
Let the compiler infer the type
Java infers the target functional interface from the assignment or method parameter context; you don't declare the type explicitly.
Step 4
Capture variables carefully
Lambdas can read local variables from the enclosing scope, but those variables must be effectively final (never reassigned).
Step 5
Use it in place of an anonymous class
Pass the lambda directly to methods like list.sort(), stream().filter(), or Thread constructors instead of a verbose anonymous inner class.
What Interviewer Expects
- Defines a lambda as an implementation of a functional interface
- Knows lambdas require exactly one abstract method on the target interface
- Explains variable capture and the effectively-final rule
- Compares lambda syntax to the older anonymous inner class approach
- Connects lambdas to the Stream API and Comparator usage
Common Mistakes
- Trying to use a lambda for an interface with more than one abstract method
- Attempting to reassign a captured local variable inside a lambda
- Confusing a lambda with a full anonymous class that can have state/fields
- Forgetting that this inside a lambda refers to the enclosing class, not the lambda itself
Best Answer (HR Friendly)
“A lambda expression is a short way in Java to write a small piece of behavior, like 'sort by this rule' or 'run this action', without writing a whole separate class for it. It makes code that used to take several lines fit on one, and it's the foundation of Java's modern stream-based data processing.”
Code Example
// Before Java 8: verbose anonymous inner class
Comparator<String> byLength = new Comparator<String>() {
@Override
public int compare(String a, String b) {
return Integer.compare(a.length(), b.length());
}
};
// With a lambda expression
Comparator<String> byLengthLambda = (a, b) -> Integer.compare(a.length(), b.length());
List<String> names = List.of("Grace", "Al", "Priya");
names.stream()
.sorted(byLengthLambda)
.forEach(System.out::println);Follow-up Questions
- What is a functional interface, and why does @FunctionalInterface matter?
- What does 'effectively final' mean for variables captured by a lambda?
- How do method references like String::toUpperCase relate to lambdas?
- How does 'this' behave inside a lambda versus inside an anonymous class?
- How do lambdas integrate with the Stream API's map and filter methods?
MCQ Practice
1. What must a target interface have for a lambda to implement it?
Lambdas can only implement functional interfaces, which by definition have exactly one abstract method.
2. What is required of a local variable captured inside a lambda?
Java requires captured local variables to be effectively final, meaning they are assigned once and never changed afterward.
3. What does a lambda expression primarily replace in older Java code?
Lambdas provide a much more concise alternative to writing an anonymous inner class for a single-method interface.
Flash Cards
What is a lambda expression in Java? — A concise anonymous implementation of a functional interface, written as (params) -> expression or a block.
What is a functional interface? — An interface with exactly one abstract method, the only kind a lambda can implement.
What does 'effectively final' mean for lambda-captured variables? — The variable is assigned once and never reassigned afterward, even without the final keyword.
Name a common functional interface used with lambdas. — Comparator<T>, Runnable, Predicate<T>, or Function<T,R> are common built-in functional interfaces.