What are lambda expressions in Java?
Lambda expressions, introduced in Java 8, are anonymous function literals that implement a functional interface inline without the boilerplate of an anonymous inner class. The syntax is: (parameters) -> expression or (parameters) -> { statements; }. When the parameter type can be inferred from context, the type declaration is optional. A single-parameter lambda can omit parentheses.
Lambdas are not objects in the traditional sense — they are instances of a functional interface inferred from the target type. The compiler determines which functional interface a lambda implements based on context: the variable type it is assigned to, the method parameter it is passed as, or an explicit cast.
A critical distinction from anonymous inner classes is how they handle the this keyword and variable capture. Inside an anonymous inner class, this refers to the inner class instance. Inside a lambda, this refers to the enclosing class instance. Both can capture variables from the enclosing scope, but those variables must be effectively final — either declared final or never reassigned after initial assignment. This prevents race conditions when lambdas are used in concurrent contexts.
Method references are a shorthand syntax for lambdas that simply delegate to an existing method: ClassName::staticMethod, instance::instanceMethod, ClassName::instanceMethod (receives the instance as first parameter), and ClassName::new (constructor reference).
Lambdas are most prominent in the Streams API: list.stream().filter(x -> x > 0).map(x -> x * 2).collect(Collectors.toList()). They also appear in CompletableFuture chains, event handlers, and anywhere a callback was previously an anonymous inner class.
Performance note: lambda creation uses invokedynamic under the hood, deferring the actual implementation strategy to the JVM. In practice this is very efficient — frequently called lambdas are compiled to static methods and inlined by the JIT.
Correctly explains syntax, target type inference, and the requirement for effectively final captured variables.
Adds the this semantics difference from anonymous inner classes, method reference forms, invokedynamic implementation detail, and concrete Streams API usage.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment