Which BiPredicate Lambda Declaration Using var Is Valid?
Which is a valid statement?
Community Votes
100% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.
Community Insight
The exam tests whether you know that var in a lambda parameter list is all-or-nothing and still requires parentheses, and the trap is picking a declaration that illegally mixes var with an explicit type like Integer or with an implicitly typed parameter.
This 1Z0-819 item tests the Java lambda parameter syntax rules for var: a lambda that declares any parameter with var must use var on every parameter and keep the parentheses. Of the four BiPredicate declarations shown, only (var x, var y) -> (x.equals(y)) satisfies JLS 15.27.1 and compiles.
Option A is the most attractive wrong answer because mixing var x with an explicit Integer y looks flexible and still reads naturally, but JLS 15.27.1 forbids mixing var with declared parameter types in the same lambda, so A does not compile.
Community Discussion (3 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
Expert Analysis
Why the Answer Is Correct
B is valid because both lambda parameters are declared with var inside the mandatory parentheses:BiPredicate testEquality = (var x, var y) -> (x.equals(y));. JLS 15.27.1 states that if one lambda parameter is declared with var, then all parameters must be declared with var, and the parameter list must remain parenthesized. Here the target type is the raw BiPredicate, so the function type is erased and the inferred parameter types are Object, which makes x.equals(y) a legal invocation. No rules on mixing, missing parentheses, or inferred types are violated, so B compiles cleanly.Why the Other Options Are Wrong
A mixesvar x with the explicit type Integer y in the same parameter list, which JLS 15.27.1 explicitly forbids — you must either use var everywhere or explicit types everywhere. C drops the parentheses (var x, var y ->...), and an implicitly typed or var-typed lambda parameter list must always be enclosed in parentheses, so this is a syntax error. D mixes var x with the implicitly typed parameter y, which is the same all-or-nothing violation as A but in the opposite direction: once var appears, every parameter needs var. None of A, C, or D can be compiled, while B is the only declaration that satisfies every lambda parameter rule.Community Comment Notes
ASPushkin reasoned through the same rule set, stating that in A "there is no mix var with the explicit types", that B is fine, and that C is a "syntax error without brackets". Another learner noted that D is a compound form that is not allowed, which matches the all-or-nothing var requirement. d7bb0b2 also chose B, adding that because theBiPredicate variable is declared without type arguments, the parameters resolve to Object rather than a specific type. A third commenter simply cross-referenced this item as similar to numbered questions 22 and 204 seen previously, suggesting the var-lambda rule recurs on the exam. Official Reference
Exam Strategy
Memorize the one-line rule — in a lambda, var is all-or-nothing and the parameter list must stay in parentheses — then scan each option for a single mixed or unparenthesized parameter. On 1Z0-819, syntax-validity questions are answered by compiling mentally, not by judging which style looks nicer.
Frequently Asked Questions
Why can't var x be mixed with an explicit Integer y in the same BiPredicate lambda?
JLS 15.27.1 makes var in lambda parameters all-or-nothing: if any parameter uses var, every parameter must use var. Mixing var with Integer is a compile-time error, so option A fails.
Does a lambda whose parameters are all declared with var still need parentheses?
Yes. Any lambda parameter list that uses var, or that is implicitly typed with more than one parameter, must be enclosed in parentheses, which is why option C's bare var x, var y -> form is a syntax error.