☕ Java
Intermediate
What are lambda expressions in Java 8?
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.
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?