
What are Java lambda expressions
- 12. Dec, 2024

What is garbage collection in
- 12. Dec, 2024

How does the `synchronized` keyword
- 12. Dec, 2024
Java lambda expressions are a feature introduced in Java 8 that provide a concise way to represent functional interfaces (interfaces with a single abstract method) using an expression instead of a full class implementation. Essentially, lambdas allow you to pass behavior (i.e., a block of code) as an argument to methods, providing more readable, less verbose code.
Syntax of Lambda Expressions:
(parameter1, parameter2, …) -> expression or block of code
Example:
// Lambda expression to add two numbers
BinaryOperator add = (a, b) -> a + b;
System.out.println(add.apply(3, 4)); // Output: 7
Runnable
, Callable
, Comparator
, and various interfaces in java.util.function
like Predicate
, Function
, etc.Example with Runnable
: