Lambdas in Java are declared similarly as arrow functions in JavaScript, using ->
instead of =>
. Unlike other variables,
lambdas cannot be declared using var
; they must provide an explicit target type.
Lambdas are assignable to functional interfaces as defined in java.util.function
and threading interfaces like Runnable
and Callable<T>
; here, we declare a simple lambda using Runnable
which prints a message when run:
Runnable lambda = () -> {
System.out.println("Hello from a lambda expression!");
};