☕ 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.