Compilation error when using var in Java submissions on LeetCode
0 reputation · 27 Mar 2021, 02:31 UTC
0 reputation · 27 Mar 2021, 02:31 UTC
Users who wish to write concise Java code with features introduced after Java 8, such as local‑variable type inference (var), switch expressions, or the HttpClient API, encounter compilation failures when submitting to LeetCode. The platform’s execution environment currently defaults to OpenJDK 8, which does not recognize these newer language constructs, leading to errors like 'cannot find symbol' or 'source version 1.8' messages. While the FAQ confirms the JDK 8 baseline, LeetCode has not announced a timeline for upgrading to a newer JDK, leaving the compatibility decision unresolved. This creates uncertainty for developers who rely on modern idioms and wonder whether they can enable a higher language level or must restrict themselves to Java 8 syntax.
33775 reputation · 27 Mar 2021, 07:03 UTC
Your code is probably fine — the compiler is not. LeetCode's Java environment compiles submissions at the Java 8 language level, and var (local-variable type inference) was only introduced in Java 10. The same applies to switch expressions (Java 12+, standardized in 14) and the java.net.http.HttpClient API (Java 11). There is currently no user-facing setting to select a newer JDK, and no officially announced upgrade timeline, so the only reliable fix inside LeetCode is to write Java 8-compatible code.
Confirmed: var is a Java 10 language feature. A compiler running with source level 1.8 will reject it, typically with cannot find symbol (it parses var as an identifier) or an explicit source version 1.8 style error. LeetCode's own FAQ documents a JDK 8 baseline for Java submissions.
Likely, but worth verifying: The exact error text you see depends on where var appears. If the message mentions cannot find symbol: class var, that's the classic Java 8 signature. If it mentions source/target versions, the platform is telling you the language level directly. Either way, the root cause is the same.
--release 11 cannot be injected through the submission interface.// var (Java 10+) -> explicit type
var map = new HashMap<String, Integer>();
// becomes
Map<String, Integer> map = new HashMap<>();
// switch expression (Java 14) -> classic switch
int n = switch (x) { case 1 -> 10; default -> 0; };
// becomes
int n;
switch (x) {
case 1: n = 10; break;
default: n = 0;
}Note that the diamond operator <> is fine — it's Java 7. Most LeetCode solutions lose very little by using explicit types; var is largely a readability convenience.
Reproduce locally to confirm the feature itself works and the failure is environmental:
javac --release 8 Solution.java # should fail on var, matching LeetCode
javac --release 11 Solution.java # should compileIf the Java 11 build succeeds, your logic and syntax are correct and the LeetCode error is purely the JDK 8 target. If you want to practice modern Java idioms, use a local JDK or another online judge that advertises a newer Java version, then backport to Java 8 syntax when submitting to LeetCode.
There is no switch to flip. Until LeetCode upgrades its Java toolchain — which it has not committed to — restrict submissions to Java 8 syntax: explicit type declarations, traditional switch statements, and no post-8 standard-library APIs like HttpClient.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.