Java Offers Several Preview Features

Java Offers Several Preview Features

A preview feature in Java is one that is not yet added permanently even though its design, specification, and implementation are complete. Presently Java has quite a few preview features in the pipeline.

Statements Before Super(...) in a Constructor

You may already know that if a class constructor is to invoke a constructor from a superclass, the super(...) statement must be the first statement in the constructor. This may change as indicated by a preview feature. In Java 22 you can specify a super(...) statement later in a constructor after other statements. The rationale behind its support is that it is not uncommon that a constructor needs to validate the arguments that it passes to the superclass constructor before calling super(...). Other requirements for this feature occur if a constructor needs to prepare the arguments in some way before passing them on to the superclass constructor, or if the superclass constructor arguments need to share a computed value.

An API for Class-File

As a developer you are probably more used to APIs meant for developing Java applications. But, did you know at times it becomes necessary to use an API to process the compiled binaries, the class files? Java Frameworks make changes to bytecode more often than individual developers do. A framework may need to parse, manipulate, and transform the .class files. Or, it may need to generate .class files directly. Presently, the JDK relies on ASM: a Java bytecode manipulation framework, which is used in the Nashorn Compiler. JDK22 includes a Class-File API for parsing, generating, and transforming Java class files. The API can track the class file format and evolve with it. It would reduce the burden on frameworks that make class-file operations dynamically to rely on third-party class-file libraries such as ASM that are not always in sync with the latest class-file format.

String Templates

You may need to compose strings by embedding expressions in literal text, and know their value only at runtime. The existing alternatives for string composition such as string concatenation have drawbacks such as being hard-to-read, verbose, and excessive boiler-plate code. String templates let you combine literal text with embedded expressions with enhanced readability, validation, and transformation of values of the embedded expressions, and flexible formatting.

Stream Gatherers

This feature adds support for custom intermediate operations in a stream of data within the Stream API. Existing support for built-in intermediate operations is rigid and lacking in many regards. An example intermediate operation that can't be performed without this preview feature is to group stream elements into fixed-size groups and retain say only the first 3 of these groups. This feature adds support for intermediate stream operations that process the elements using a "gatherer", which represents a transform of the elements of the stream. Gatherer operations can be performed either sequentially or in parallel.

Two other preview features are Scoped Values and Structured Concurrency discussed in Java 20 Brings Scoped Values to Core-Libs and JDK 20 Introduces Structured Concurrency respectively.

Up Next

About the Author

TechWell Insights To Go

(* Required fields)

Get the latest stories delivered to your inbox every month.