Lambda syntax
Description
Parameters are followed by an arrow and body.
Explanation
The target functional interface supplies the parameter and return types.
Example
Runnable task = () -> System.out.println("Study");← Chapter 7 — Input, Wrappers & Lambdas
mixed
A lambda expression is a compact way to provide behavior for a functional interface. A functional interface has exactly one abstract method.
Lambdas are common with collections, streams, and event handling. They work best when the action is short and has a descriptive surrounding context.
Parameters are followed by an arrow and body.
The target functional interface supplies the parameter and return types.
Runnable task = () -> System.out.println("Study");They represent one operation.
Runnable, Comparator, and Predicate are common examples.
java.util.function.Predicate<Integer> pass = s -> s >= 40;A lambda can capture local variables only when they are final or effectively final. This restriction keeps behavior predictable.
public class LambdaDemo {
public static void main(String[] args) {
Runnable reminder = () -> System.out.println("Aditi, review Java today!");
reminder.run();
}
}