Java Version 22 Released
Written by Nikos Vaggalis   
Thursday, 04 April 2024

JDK 22 is not a Long Term Support release, but is one of the regular releases that are scheduled to arrive every six months. Still, it has got a lot to show for itself.

This release has got a bit of everything; it is comprised of JEPs in final, preview as well as incubator state. As a refresher a JEP stands for JDK Enhancement Proposal. They are proposals for adding new major features to the JDK. Arriving at a specification from of an initial proposal is an iterative process which also includes Requests For Comments.

A proposed feature can concern the addition of something new to the language, like Records or Switch expressions, or to any of the Java APIs. Looking at JEPs can answer a lot of questions, like discovering when a certain feature was introduced or looking at where things are heading in the future. As such, the JEPs this release is about are:

423: Region Pinning for G1
447: Statements before super(. . . ) (Preview)
454: Foreign Function & Memory API
456: Unnamed Variables & Patterns
457: Class-File API (Preview)
458: Launch Multi-File Source-Code Programs
459: String Templates (Second Preview)
460: Vector API (Seventh Incubator)
461: Stream Gatherers (Preview)
462: Structured Concurrency (Second Preview)
463: Implicitly Declared Classes and Instance Main Methods (Second Preview)
464: Scoped Values (Second Preview)

All great but let's examine the most eye catching.

454: Foreign Function & Memory API
This JEP lays out a new way to access native libraries that are written in languages like C and C++. Under this API Java programs can call native libraries and process native data without the brittleness and dangers of JNI.

447: Statements before super()
A feature in preview which allows statements that do not reference the instance being created to appear before an explicit constructor invocation.

This is helpful when we need to validate an argument that is passed to a superclass constructor by allowing the inclusion of the validation logic directly in the constructor as:

something that results in a more flexible and readable code.

Another feature in preview is that of 457: Class-File API, which aims to provide a standard API for working with Java class files.
It is well known fact that the Java ecosystem has many libraries for parsing and generating class files, each one with different design goals, strengths, and weaknesses. This API is an attempt to remedy that fragmentation by providing a unified way for working with those files.

459: String Templates
If you've ever tried to write a SQL multi line query inside Java code using the + operator, then you're aware of the pain.
This has been solved as far as Java 14 though with the introduction of Text Blocks. What string templates offer as extra is that you can now also include string literals and variable values whose values will be injected at runtime but less prone to introducing security vulnerabilities. For instance:

String weather = "Sunny";
  System.out.println(STR."The weather will be \{weather} tomorrow.");

And finally JEP 461: Stream Gatherers
The use of streams in the Java ecosystem is by now pervasive, and ideal for many tasks, but the fixed set of intermediate operations means that some complex tasks cannot easily be expressed as stream pipelines. Either a required intermediate operation does not exist, or it exists but does not directly support the task.

Stream::gather(Gatherer) is a new intermediate stream operation that processes the elements of a stream by applying a user-defined entity called a gatherer. With the gather operation we can build efficient, parallel-ready streams that implement almost any intermediate operation. Stream::gather(Gatherer) is to intermediate operations what Stream::collect(Collector) is to terminal operations. For instance, a gatherer can transform one input element to one output element until some condition becomes true, at which time it starts to transform one input element to two output elements.

That's just a bird's eye view of the new release. In case you want to delve deep as low as the API level, you must check Javaalmanac which lists all the low-level API modifications or additions between JDK versions.

In summary, Java is still evolving and this version offers a few more reasons to go for it if you have not already.

 

More Information

JDK 22

Javaalmanac-New APIs in Java 22
https://javaalmanac.io/jdk/22/apidiff/21/ 

Related Articles

The I Programmer Java 2023 Recap  

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Nobel Prize For Chemistry For AlphaFold
11/10/2024

The Royal Swedish Academy of Sciences has awarded a half share of the 2024 Nobel Prize For Chemistry to Demis Hassabis, CEO of Google DeepMind and his colleague John Jumper for "protein stru [ ... ]



Eclipse Foundation Launches Open Regulatory Compliance Working Group
24/09/2024

The Eclipse Foundation has announced the formation of the Open Regulatory Compliance Working Group (ORC WG), which they say will support participants across the open source community in navigating and [ ... ]


More News

kotlin book

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Thursday, 04 April 2024 )