Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

What is a lambda expression in Java, and what determines its type? Explain target typing with an example where the same lambda has two different types.

A lambda expression is an anonymous function that provides the implementation of the single abstract method of a functional interface. It has no type of its own: its type is the target type, the functional interface that the surrounding context expects, such as the type of the variable it is assigned to, the method parameter it is passed to, or the return type. So x -> x.isEmpty() is a Predicate<String> in one place and a Function<String, Boolean> in another, and var f = x -> x; does not compile because there is no target to give it a type.

Why this question is asked

Lambdas are everywhere in modern Java, in streams, in Spring configuration, in CompletableFuture chains, and most of the time we write them without thinking about their type. But the moment something goes wrong, such as an overload that will not resolve or a var that refuses to compile, we need to know where the type comes from. Interviewers ask this question because target typing is the one idea that explains all of those surprises. In this answer, we will recap functional interfaces, see how the compiler gives a lambda its type from the context, write the same lambda with two different types, look at why var cannot hold a lambda, and finish with the ways to write parameter types.

1A Quick Recap of Functional Interfaces

A functional interface is an interface with exactly one abstract method. Runnable has run(), Comparator<T> has compare(T, T), and Predicate<T> has test(T). It may have any number of default or static methods, because those already have bodies. The annotation @FunctionalInterface is optional; it only asks the compiler to check the single-method rule.

A lambda expression is a short way to write an implementation of that one method without writing a class. The arrow separates the parameters from the body. When we write s -> s.length() > 3, we are writing the body of some functional interface's method; the lambda itself does not say which interface. That is the key point of this whole question.

Java
@FunctionalInterface
interface Validator { boolean check(String s); }
Validator notBlank = s -> !s.isBlank();
System.out.println(notBlank.check(" ")); // false
System.out.println(notBlank.check("ok")); // true

2Where a Lambda Gets Its Type

Java is a statically typed language, so every expression must have a type at compile time. For most expressions the type comes from the expression itself: 42 is an int, "a" is a String. A lambda is different. The compiler looks at the context the lambda appears in and asks what type is expected there. That expected type is called the target type. The compiler then checks that it is a functional interface, and that the lambda's shape fits its method: the same number of parameters, a compatible return, and no checked exceptions the method does not declare.

Several contexts provide a target type. An assignment or variable declaration gives the variable's type. A method argument gives the parameter's type, after overload resolution. A return statement gives the method's return type. A cast gives the cast type. The branches of a conditional expression get the type expected for the whole expression.

Contexts that give a lambda its target type
ContextExampleTarget type
AssignmentPredicate<String> p = s -> s.isEmpty();Predicate<String>
Method argumentlist.removeIf(s -> s.isEmpty());Predicate<? super String>
Returnreturn s -> s.isEmpty(); // in a method returning Predicate<String>Predicate<String>
Cast(Predicate<String>) s -> s.isEmpty()Predicate<String>
ConditionalRunnable r = flag ? () -> a() : () -> b();Runnable

3One Lambda, Two Types

Here is the same text, x -> x.isEmpty(), placed in two different contexts.

Java
Predicate<String> p = x -> x.isEmpty(); // target: Predicate<String>, boolean test(String)
Function<String, Boolean> f = x -> x.isEmpty(); // target: Function<String, Boolean>, Boolean apply(String)
System.out.println(p.test("")); // true (a primitive boolean)
System.out.println(f.apply("abc")); // false (a Boolean object, boxed automatically)
p = f; // does not compile: incompatible types, Function<String,Boolean> cannot be converted to Predicate<String>
Step throughHow the compiler types x -> x.isEmpty()
  1. Find the target type from the contextfor example Predicate<String>
  2. Is it a functional interface?
    YesFind its single abstract methodNoCompile error
  3. Give the parameters the method's typesx becomes a String
  4. Do the body's result and exceptions fit the method?
    YesThe lambda has that typeNoCompile error

4 steps in order. Play them, or step through them one at a time.

In the first line, the compiler sees that Predicate<String> has boolean test(String), so it gives x the type String and checks that x.isEmpty() returns a boolean. In the second line, it sees Boolean apply(String), gives x the type String again, and accepts the boolean result because it can be boxed to Boolean.

Notice the last line. Even though both objects were created from identical lambda text, p and f have unrelated types, and we cannot assign one to the other. The type was decided at each lambda's own position, from its own context, and it stays fixed. To convert, we would write p = f::apply; which creates a new Predicate whose test method calls f.apply.

4Why var Cannot Hold a Lambda

var asks the compiler to infer a variable's type from the expression on the right. A lambda needs a type from the left. Each one is waiting for the other, so the compiler has nothing to work with.

Java
var f = x -> x;
// error: cannot infer type for local variable f
// (lambda expression needs an explicit target-type)
Object o = () -> {};
// error: incompatible types: Object is not a functional interface
var r = (Runnable) () -> System.out.println("hi"); // compiles: the cast is the target
r.run(); // hi

The Object case fails for the same reason seen from the other side: there is a target type, but it is not a functional interface, so the compiler cannot know which method the lambda implements. A cast fixes both cases, because the cast itself becomes the target type.

5Writing the Parameter Types

Usually we leave the parameter types out and let the compiler take them from the target, which is called an implicitly typed lambda. We can also write them, which helps readability in long lambdas or when inference picks something we did not intend. Since Java 11 we can write var for each parameter, which is useful mainly when we want to add an annotation without spelling out a long type.

Java
BiFunction<String, Integer, String> repeat1 = (s, n) -> s.repeat(n); // inferred
BiFunction<String, Integer, String> repeat2 = (String s, Integer n) -> s.repeat(n); // explicit
BiFunction<String, Integer, String> repeat3 = (var s, var n) -> s.repeat(n); // var, Java 11+
BiFunction<String, Integer, String> repeat4 = (@NonNull var s, var n) -> s.repeat(n);
System.out.println(repeat1.apply("ab", 3)); // ababab
// (String s, n) -> ... does not compile: all parameters explicit, or none
// (var s, String n) -> ... does not compile: cannot mix var and explicit types

The rule is all or nothing: every parameter is inferred, every one has an explicit type, or every one uses var. Mixing styles does not compile. When the types are written explicitly, they must match the target method's parameter types exactly; the compiler does not convert them.

6Follow-up questions

Is a lambda just an anonymous inner class?

No, although it fills the same role. An anonymous class creates a new class with its own this, can have fields and several methods, and always holds a reference to the enclosing instance. A lambda's this means the enclosing instance, it has no state of its own beyond captured values, and it is compiled with invokedynamic rather than as a separate class file.

Why does a method reference behave the same way?

Because it is also a poly expression with no type of its own. String::isEmpty can be a Predicate<String> or a Function<String, Boolean> depending on the target, exactly like the lambda, and var g = String::isEmpty does not compile for the same reason.

Can target typing cause ambiguity?

Yes, with overloaded methods that accept different functional interfaces. If a method has overloads taking Callable and Runnable, or Predicate and Function, the compiler must pick one from the lambda's shape, and sometimes it picks one we did not expect or reports an ambiguity. A cast or an explicitly typed lambda resolves it.

7Conclusion

In this answer, we saw that a lambda is an anonymous function implementing the single abstract method of a functional interface, and that it has no type of its own. Its type comes from the target type that the context provides: an assignment, a method argument, a return, a cast or a conditional. That is why x -> x.isEmpty() can be a Predicate<String> or a Function<String, Boolean>, why the two results cannot be assigned to each other, and why var f = x -> x; does not compile. Parameter types are inferred from the target, or written explicitly, or written with var, but all in the same style.

What to say in the interviewAbout 90 seconds aloud

Open with

A lambda expression is an anonymous function: a short way to implement the single abstract method of a functional interface, such as Predicate's test or Runnable's run. The important part is that a lambda has no type of its own.

Then, if they want more

Its type is the target type, the functional interface the context expects. That context can be an assignment or variable declaration, a method argument, a return statement, a cast, or the branches of a conditional.

The compiler takes the target, finds its one abstract method, gives the lambda's parameters that method's parameter types, and checks that the body returns something compatible and throws no checked exceptions the method does not declare.

So the same text, x -> x.isEmpty(), is a Predicate<String> if I assign it to a Predicate, where test returns a primitive boolean, and a Function<String, Boolean> if I assign it to a Function, where the result is boxed. Those two objects then have unrelated types; I cannot assign one to the other without something like f::apply.

It also explains why var f = x -> x does not compile: var needs a type from the right side and the lambda needs one from the left, so I need a cast or an explicit type. Parameter types are inferred by default, or I can write them explicitly or with var since Java 11, but not mix styles.

About this answer

Last updated
12 November 2026
Versions it names
Java 11
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is a lambda expression in Java, and what determines its type? Explain target typing...": https://scrivio-interview-library.netlify.app/java/lambdas/fa3a138705/

More in Lambdas and method references