Functional Interfaces and Lambdas in Java
Fast recall
Quick revision
Core answer
Whether you understand the relationship between lambdas and functional interfaces, what @FunctionalInterface enforces, and the key built-in types (Predicate, Function, Consumer, Supplier)
Thinking lambdas are just syntactic sugar for anonymous inner classes — they differ in how `this` is captured (outer class vs the anonymous class itself) and in how they are compiled (invokedynamic vs a new class file)
Explain variable capture rules (effectively final), contrast lambda `this` with anonymous class `this`, name the key java.util.function types and their signatures, and mention method references as an even more concise alternative
Interview-ready guide
Interview answer
A functional interface has exactly one abstract method — Runnable, Comparator, Predicate, Function. A lambda provides the implementation of that single method inline.
Lambdas vs anonymous inner classes:
- Lambdas use
invokedynamic—LambdaMetafactorygenerates a lightweight class at runtime, reusable across invocations; no separate.classfile - Anonymous classes generate a separate
.classfile at compile time with their own constructor andthisreference - Lambda
thisrefers to the enclosing class; anonymous classthisrefers to itself
Variable capture:
- Captured local variables must be effectively final — lambda copies the value, not a reference
- Instance fields have no restriction — accessed through the captured
thisreference
Standard functional interfaces:
Predicate— filter condition; composes with.and(),.or(),.negate()Function— transformation; composes with.andThen(),.compose()Consumer— side effect (returns void); chains with.andThen()Supplier— lazy creation (takes no input); critical fororElseGet()
I recommend lambdas for pure transformations and method references for readability — String::toUpperCase is clearer than s -> s.toUpperCase().
Functional Interfaces and Lambdas in Java
Direct Answer
A functional interface is any interface with exactly one abstract method (SAM — Single Abstract Method). A lambda expression is a concise inline implementation of that one method. The compiler converts the lambda into an instance of the functional interface using invokedynamic, which defers the actual class generation to runtime for better performance.
Common Mistakes & Pitfalls
Thinking lambdas are just syntactic sugar for anonymous inner classes — they differ in how this is captured (outer class vs the anonymous class itself) and in how they are compiled (invokedynamic vs a new class file)
Check your understanding
Answer these without looking back at the article.
- What is the difference between a lambda and an anonymous inner class?
- What are the four main method reference forms?
- How do you compose two Function instances using andThen and compose?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.