← Back to Java

Lambda Expressions

JavaMidawsjava

The Question

What are lambda expressions in Java?

What a Strong Answer Covers

  • anonymous functions Java 8
  • implement functional interfaces
  • (params) -> expression syntax
  • replaces anonymous inner classes
  • works with Stream API
  • method references as shorthand

Senior-Level Answer

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.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Correctly explains syntax, target type inference, and the requirement for effectively final captured variables.

3/3 — Strong Answer

Adds the this semantics difference from anonymous inner classes, method reference forms, invokedynamic implementation detail, and concrete Streams API usage.

Common Mistakes

  • Saying captured variables must be declared final — they only need to be effectively final (never reassigned), not explicitly declared final.
  • Thinking lambdas are just syntactic sugar for anonymous inner classes — they differ in this semantics and are implemented via invokedynamic, not class generation.
  • Not being able to name the four method reference forms (static, bound instance, unbound instance, constructor).

Follow-Up Questions

  • Why must variables captured by a lambda be effectively final? — To prevent race conditions in concurrent use and to maintain referential transparency — the lambda could outlive the stack frame where the variable lived.
  • What is the difference between a lambda and a method reference? — A method reference is a shorthand lambda that delegates to an existing named method. They are interchangeable when the lambda body is only a method call with matching parameters.
  • How does invokedynamic make lambda creation more efficient than anonymous inner classes? — invokedynamic defers the implementation strategy to the JVM at runtime, allowing the JIT to compile non-capturing lambdas to static methods that are allocated once and reused.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

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
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout