Java · OOP, JVM and backend craft
What is a lambda expression in Java?
Short answer
A lambda is a concise implementation of a functional interface. It captures effectively final local variables and lets you pass behaviour as a value.
A lambda has a parameter list, an arrow and a body. It has no explicit type name; the target type comes from the context, such as a Comparator or Runnable variable. The compiler checks that the target is a functional interface with one abstract method.
Comparator<String> byLength =
(left, right) -> Integer.compare(left.length(), right.length());
Runnable task = () -> System.out.println("run");Capture and scope
- A lambda can read local variables that are effectively final
- It cannot change a captured local variable
- this inside a lambda refers to the enclosing instance, unlike an anonymous class
- Lambdas are compiled with invokedynamic rather than a class per expression
Lambdas reduce ceremony, especially for callbacks and stream pipelines. They are not always clearer: a long lambda with branching logic is better extracted into a named method. A method reference such as Employee::name is often more readable than a trivial lambda.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why must captured local variables be effectively final?
- 2How does this differ between a lambda and an anonymous class?
- 3When would you use a method reference instead?
- 4How are lambdas implemented at runtime?
Say it in the interview
A lambda is a short implementation of a functional interface. The target type gives the parameter types, and the body can be an expression or a block. It can capture effectively final locals, and this refers to the enclosing instance. I use lambdas for small callbacks and stream steps, but I extract longer logic into named methods and prefer method references when they read more clearly.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Java mock interview and get scored feedback on depth, clarity and confidence.