Behavior of var with Lambda Parameters in Java 11+ for OCP
Posted: Wed Nov 12, 2025 10:27 pm
Hi all,
While studying for the OCP exam, I noticed that Java 11 allows using var for lambda parameters. I’m a bit confused about how this affects type inference and annotations.
For example, in a lambda like (var x, var y) -> x + y, does using var change anything in terms of type safety or compilation compared to explicitly specifying the parameter types? Also, are there any scenarios in the exam where using var in lambdas could lead to a tricky question?
Retro Bowl
Would really appreciate a clear explanation or example.
Thanks!
While studying for the OCP exam, I noticed that Java 11 allows using var for lambda parameters. I’m a bit confused about how this affects type inference and annotations.
For example, in a lambda like (var x, var y) -> x + y, does using var change anything in terms of type safety or compilation compared to explicitly specifying the parameter types? Also, are there any scenarios in the exam where using var in lambdas could lead to a tricky question?
Retro Bowl
Would really appreciate a clear explanation or example.
Thanks!