Java 17 Interview Questions and Answers
Related differences
Ques 16. What is Switch Pattern Matching in Java 17?
It makes switch statements and expressions more flexible and expressive by letting patterns appear in case labels. It also makes it possible to relax the switch's null-hostility when needed.
There are two main patterns, which are as follows:
- Guarded pattern: Uses pattern && boolean expression to refine further.
- Pattern between parentheses
static record Human (String name, int age, String profession) {}
public String checkValue(Object obj) {return switch (obj) {case Human h -> "Name: %s, age: %s and profession: %s".formatted(h.name(), h.age(), h.profession());case Circle c -> "This is a circle";case Shape s -> "It is just a shape";case null -> "It is null";default -> "It is an empty";};}
public String checkShape(Shape shape) {return switch (shape) {case Triangle t && (t.getNumberOfSides() != 3) -> "This is a weird triangle";case Circle c && (c.getNumberOfSides() != 0) -> "This is a weird circle";default -> "Just a normal shape";};}
Ques 17. What is Generate sealed Classes in Java 17?
Adds sealed classes and interfaces to the Java language to make it better. Sealed classes and interfaces limit other classes, and interfaces can extend and implement them, respectively.
Showing some other progress to pattern matching along with the JEP 406 will make it possible to check the type, cast, and act code pattern more sophisticatedly and cleanly.
public abstract sealed class Subjects
permits English, Maths, Science {...}
Ques 18. Where can we download Java 17?
You can download Java 17 from the Oracle website: https://www.oracle.com/java/technologies/downloads/#java17
Ques 19. Provide an example of Vector API update in Java 17.
The example is as follows:
public void newVectorComputation(float[] a, float[] b, float[] c) {for (var i = 0; i < a.length; i += SPECIES.length()) {var m = SPECIES.indexInRange(i, a.length);var va = FloatVector.fromArray(SPECIES, a, i, m);var vb = FloatVector.fromArray(SPECIES, b, i, m);var vc = va.mul(vb);vc.intoArray(c, i, m);}}
public void commonVectorComputation(float[] a, float[] b, float[] c) {for (var i = 0; i < a.length; i ++) {c[i] = a[i] * b[i];}}
Ques 20. What is the next LTS version after Java 17?
The next LTS (Long Time Support) version is Java 21 and is released in September 2023.
Most helpful rated by users:
- What is Java 17 or Java 17 in nutshell?
- What are the JDK17 new features?
- What is the difference between Oracle JDK and OpenJDK?