Java FAQ: Top Questions
23. What are lambda expressions in Java?
Lambda expressions , introduced in Java 8, provide a concise way to represent anonymous functions, enabling functional programming. They are used primarily with functional interfaces (interfaces with one abstract method).
-
Syntax:
(parameters) -> expression
or{ statements; }
. -
Functional Interface:
e.g.,
Runnable
,Comparator
, or custom interfaces with@FunctionalInterface
. - Use Cases: Simplifies code for event handling, collections, and streams.
- Benefits: Reduces boilerplate compared to anonymous classes.