Java 17 and 21 Features Interview Questions
Most companies have moved from Java 8 to Java 17 or 21, and Spring Boot 3 requires Java 17, so interviewers now expect you to know modern Java. This topic covers the features asked about most: var, records and compact constructors, sealed classes, switch expressions, pattern matching for instanceof and switch, record patterns, text blocks, sequenced collections, and the new String, Stream and Optional methods.
It finishes with virtual threads from Java 21: how they differ from platform threads, how to use them in Spring Boot, and when not to use them. Every answer includes a complete program you can run on Java 21, with its real output.
- What are the differences between Java 8, 11, 17 and 21, and why should a project upgrade?
- What is the var keyword in Java 10, and when should you avoid it?
- What are records in Java, and when should you use them?
- Can a record have custom constructors, methods and validation?
- What is the difference between a record, a regular class and a Lombok @Data class?
- What are sealed classes and interfaces in Java 17?
- What are switch expressions in Java, and how are they different from the old switch statement?
- What is pattern matching for instanceof in Java 16?
- What is pattern matching for switch in Java 21?
- What are record patterns in Java 21?
- What are text blocks in Java?
- What are sequenced collections in Java 21?
- Which useful String, Stream and Optional methods were added after Java 8?
- What is the Java 11 HttpClient, and how is it better than HttpURLConnection?
- What are helpful NullPointerExceptions in Java 14+?
- What are virtual threads in Java 21, and how are they different from platform threads?
- When should you not use virtual threads, and what is thread pinning?
- How do you create and use virtual threads, including in Spring Boot?