0% found this document useful (0 votes)
2 views

Java_21_Interview_Problems

Java 21 introduces key enhancements including virtual threads, pattern matching for switch, scoped values, and sequenced collections, aimed at improving concurrency, expressiveness, and performance. Virtual threads allow for lightweight task management, while pattern matching simplifies conditional logic. Scoped values enhance data sharing across call chains, and sequenced collections provide better access to ordered elements.

Uploaded by

pbecic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java_21_Interview_Problems

Java 21 introduces key enhancements including virtual threads, pattern matching for switch, scoped values, and sequenced collections, aimed at improving concurrency, expressiveness, and performance. Virtual threads allow for lightweight task management, while pattern matching simplifies conditional logic. Scoped values enhance data sharing across call chains, and sequenced collections provide better access to ordered elements.

Uploaded by

pbecic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java 21 for Strong Senior Developers

What are the key enhancements in Java 21 and why are they important?
 Java 21 introduces several important features as part of long-term support (LTS):
 Virtual threads (Project Loom).
 Record patterns and pattern matching for switch.
 Scoped values.
 Sequenced collections.
 String templates (preview).
 These features aim to improve concurrency, expressiveness, and performance.

What are virtual threads and how do they improve concurrency models in Java
21?
 Virtual threads are lightweight threads managed by the JVM, not tied to OS threads.
 Benefits:
 Support millions of concurrent tasks with minimal resource usage.
 Remove need for complex thread-pooling or reactive programming in many cases.
 Key points:
 Use Thread.startVirtualThread() or Executors.newVirtualThreadPerTaskExecutor().
 They maintain the same programming model as platform threads (same APIs).
 Ideal for I/O-bound workloads such as web servers and message processing.

What is pattern matching for switch in Java 21 and how does it help?
 Pattern matching simplifies conditional logic and type checks.
 Pattern matching for switch allows matching on type and deconstruction in one
expression.
 Example:
 switch (obj) {
case String s -> System.out.println("String: " + s);
case Integer i -> System.out.println("Int: " + i);
default -> System.out.println("Unknown");
}
 Benefits:
 Reduces boilerplate of instanceof + casting.
 Improves clarity and maintainability.

What are scoped values and how do they differ from ThreadLocal?
 Scoped values provide a structured way to share immutable data across call chains.
 Similar to ThreadLocal but:
 Safer — cannot leak across thread boundaries.
 Optimized for virtual threads.
 Read-only by design — promotes immutability.
 Useful for context propagation (e.g., tracing IDs, user sessions) in virtual thread
applications.

What are sequenced collections and why were they introduced in Java 21?
 Sequenced collections are new interfaces (SequencedCollection, SequencedMap,
SequencedSet) that provide first/last element access.
 Motivation:
 Unify collections that maintain order — e.g., LinkedList, LinkedHashSet,
LinkedHashMap.
 New methods:
 getFirst(), getLast(), addFirst(), addLast(), reversed().
 Benefits:
 More expressive APIs for ordered collections.
 Improves consistency across collection types.

You might also like