Java Interview Questions on Java SE 22
-
What are Unnamed Variables and Patterns and how do they simplify Java code?
Answer:
Java 22 finalizes support for Unnamed Variables and Patterns (JEP 456). You can use the underscore (_
) to omit variable names in contexts where the variable is unused—such as in exception handlers, lambda parameters, or pattern matching, reducing boilerplate and improving readability.
-
What is the Foreign Function and Memory API and why is it significant?
Answer:
The Foreign Function and Memory API (JEP 454) is finalized in Java 22. It replaces the older JNI by offering a safer, lower-overhead way for Java to call native code and manage off-heap memory (via MemorySegment
, Arena
, etc.), improving performance and ergonomics.
-
Describe Scoped Values and their advantage over thread-local variables.
Answer:
Scoped Values (JEP 464, second preview) allow immutable, inheritable values within a confined scope (e.g., a block or thread). They're automatically cleaned up and have better safety and simplicity than traditional thread-local variables.
-
What does Structured Concurrency bring to Java threading?
Answer:
Structured Concurrency (JEP 462, second preview) simplifies concurrent programming by managing related tasks as structured units (e.g., via StructuredTaskScope
), improving readability, error handling, and observability for multithreaded workflows.
-
What are Stream Gatherers?
Answer:
Stream Gatherers (JEP 461, preview) enable writing custom intermediate stream operations, similar to how collectors work for terminal operations, allowing more flexible stream pipelines.
-
Explain the feature Statements Before super(...) in constructors
Answer:
JEP 447 (preview) allows executing statements before calling super(...)
or this(...)
in constructors. This gives developers more flexibility for initialization logic, under controlled constraints.
-
What is Launch Multi-File Source-Code Programs?
Answer:
JEP 458 enables running Java programs composed of multiple .java files directly—without needing prior compilation. This improves rapid prototyping and simplifies running small multi-file examples.
-
What is the Class-File API introduced in Java 22?
Answer:
The Class-File API (JEP 457, preview) offers a set of APIs to parse and generate Java .class
files programmatically, useful for tools like analyzers, compilers, and bytecode instrumentation utilities.
-
What enhancement does Region Pinning for G1 (JEP 423) provide?
Answer:
Region Pinning (JEP 423) ensures that G1 garbage collector won't move certain memory regions, especially during JNI critical operations—preventing GC pauses and improving performance in native interactions.
-
Which preview features were carried forward into Java 22 (second preview)?
Answer:
Java 22 continues the second preview for several features, namely: Structured Concurrency (JEP 462), Scoped Values (JEP 464), String Templates (JEP 459), and Implicitly Declared Classes and Instance Main Methods (JEP 463).
