What are lambda expressions in Java 8?
Why Interviewers Ask This
This tests whether you can apply Java knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.
Answer
Lambda expressions, introduced in Java 8, provide a concise way to represent an instance of a functional interface (an interface with exactly one abstract method) using an anonymous function. Syntax: (parameters) -> expression or (parameters) -> { statements; }. Example: list.sort((a, b) -> a.compareTo(b)) instead of an anonymous Comparator class. Lambdas can capture effectively-final local variables from the enclosing scope. They enable functional programming patterns and make code significantly more readable, especially with streams and collection operations. Under the hood, lambdas are implemented using invokedynamic bytecode instructions, not as anonymous inner classes.
Pro Tip
This topic has Java-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.
More Java Questions
View all →- Intermediate What is the Java Collections Framework?
- Intermediate What is the difference between ArrayList and LinkedList?
- Intermediate What is HashMap in Java and how does it work internally?
- Intermediate What is the difference between HashMap and HashTable in Java?
- Intermediate What is the difference between HashMap and LinkedHashMap?