Java 21 Interview Questions and Answers
Ques 6. Please provide example of using Sequence Collections in Java 21.
Here is an example of accessing the First and Last Element in Java 21:
Prior to JDK 21, retrieving the first and last elements of collections in Java involved different methods and approaches depending on the collection type. Let’s examine some examples of accessing the first and last elements using the pre-JDK 21 JDK API calls:
For List:
First Element: list.get(0)
Last Element: list.get(list.size()-1)
For Deque:
First Element: deque.getFirst()
Last Element: deque.getLast()
For Set:
First Element: set.iterator().next() or set.stream().findFirst().get()
Last Element: requires iterating through the set
For SortedSet:
First Element: set.first()
Last Element: set.last()
With the introduction of JDK 21 and the Sequenced Collections feature, accessing the first and last elements becomes more consistent and straightforward. The new methods simplify the process across different collection types:
For List, Deque, Set:
First Element: collection.getFirst()
Last Element: collection.getLast()
Ques 7. What are Record Patters in Java 21?
Enhance the Java programming language with record patterns to deconstruct record values. Record patterns and type patterns can be nested to enable a powerful, declarative, and composable form of data navigation and processing.
Example of Pattern matching and records:
// As of Java 16record Point(int x, int y) {}static void printSum(Object obj) {if (obj instanceof Point p) {int x = p.x();int y = p.y();System.out.println(x+y);}}
Updates in Java 21:
// As of Java 21static void printSum(Object obj) {if (obj instanceof Point(int x, int y)) {System.out.println(x+y);}}
Ques 8. What is Pattern Matching for switch in Java 21?
Enhanced the Java programming language with pattern matching for switch expressions and statements.
Extending pattern matching to switch allows an expression to be tested against a number of patterns, each with a specific action, so that complex data-oriented queries can be expressed concisely and safely.
// Prior to Java 21static String formatter(Object obj) {String formatted = "unknown";if (obj instanceof Integer i) {formatted = String.format("int %d", i);} else if (obj instanceof Long l) {formatted = String.format("long %d", l);} else if (obj instanceof Double d) {formatted = String.format("double %f", d);} else if (obj instanceof String s) {formatted = String.format("String %s", s);}return formatted;}
Updates in Java 21:
// Java 21static String formatterPatternSwitch(Object obj) {return switch (obj) {case Integer i -> String.format("int %d", i);case Long l -> String.format("long %d", l);case Double d -> String.format("double %f", d);case String s -> String.format("String %s", s);default -> obj.toString();};}
Ques 9. What are Virtual Thread in Java 21?
Virtual threads are JVM-managed lightweight threads that will help in writing high-throughput concurrent applications (throughput means how many units of information a system can process in a given amount of time). [JEP-425, JEP-436 and JEP-444] In Java 21, virtual threads are ready for production use.
With the introduction of virtual threads, it becomes possible to execute millions of virtual threads using only a few operating system threads. The most advantageous aspect is that there is no need to modify existing Java code. All that is required is instructing our application framework to utilize virtual threads in place of platform threads.
To create a virtual thread, using the Java APIs, we can use the Thread or Executors.
Example:
Runnable runnable = () -> System.out.println("Inside Runnable");
//1
Thread.startVirtualThread(runnable);
//2
Thread virtualThread = Thread.ofVirtual().start(runnable);
//3
var executor = Executors.newVirtualThreadPerTaskExecutor();
executor.submit(runnable);
Ques 10. What are Unnamed Patterns and Variables in Java 21?
It is common in some other programming languages (such as Python, Scala) that we can skip naming a variable that we will not use in the future.
Example:
public void print(Object o) {
switch (o) {
case Point(int x, int _) -> System.out.printf("The x position is : %d%n", x); // Prints only x
//...
}
}
Most helpful rated by users: