Java Development Kit (JDK) 18 or simply Java18 has been officially released in March 2022. It is the first non-LTS long-term support release since the release of JDK 17. Despite being a short term feature release that is supported for just six months, Java18 is packed with some great new features.
In this article, we will be discussing some of the new features and enhancements introduced in the recent release of Java 18. Some notable features include the Incubation of vector API, the second preview of pattern matching for switch expressions, making UTF-8 the default character set and the inclusion of a simple web server.
· Default character Set: UTF-8
The first feature on our list is the introduction of UTF-8 as the default character set. For those who might not know, UTF-8 is a variable-length character encoding commonly used on the web for electronic communication. It is also considered the standard character set for the web as it is capable of encoding every character that is used on the web. Previously, the default charset was used to be determined by the host operating system and locale but it could lead to some inconsistencies in the APIs. Now the APIs that depend on the default charset will be consistent across all implementations, operating systems and locales due to a fixed character set. The UTF-8 is the most widely used character set in the world, so making it a default choice in Java18 would simplify things quite a lot.
Windows, macOS, several Linux distributions, and many server applications already support UTF-8 so it seems to have no issues for the vast majority of users however, there is a slight risk in some other environments such as Asian locales where their local languages are not fully supported by UTF-8.
It is not that UTF-8 does not cover Asian locales but as UTF-8 uses a maximum of 4 bytes, depending on the character, some rare characters are might be excluded. As result, the applications depending on the default charset will not behave properly while processing the data with the UTF-8 character set.
· Addition of a Simple Web Server
The Java18 also offers a simple web server proposal. A basic web server with a command line interface that can only serve the static files. There will be no CGI (Common Gateway Interface) or servlet-like functionality available. This feature will prove to be very helpful for specific server tasks such as prototyping, testing, training and ad-hoc coding. This simple web server is also customizable and can be enhanced with new APIs for specific tasks such as server creation and request handling.
All these features are incomparable even with any basic sever tool available in the market but keeping in mind, providing a feature-filled or full-scale server is not a goal of the proposal. With this feature, it is aimed to make Java18 more approachable for developers by offering a ready to use static HTTP file server with a simple setup and minimal functionality so that users do not require any third-party server tool.
· Code Snippets in Java API Documentation
Code snippets in Java API documentation are also introduced in Java18. The introduction of an @snippet tag in the Standard Doclet will simplify and allows the inclusion of code snippets in the Java application programming interface (API) documentation. These example source codes are already a great help for new Java developers who are learning from the documentation as these code examples illustrate the use of each feature.
The authors of API documentation often include a small piece of source code in documentation comments but they are written as comments in HTML and always begin with the comment characters. It required certain changes to each code snippet to be able to be compiled, these changes can sometimes be very repetitive and tiring work. With the inclusion of the @snippet tag in Java18, the Java programmers can now directly test out these codes in their applications or they can also easily insert these small pieces of code into a more extensive program to apply a set of functionalities. By making these example codes accessible, this Java18 feature aims to familiarize the new Java developers with the modern style of code writing, as well as the use of proper names for declarations.
· Vector APIs
A vector is like an array of numbers in Java that can be manipulated as one entity. A vector computation is a series of operations on several big-scale vectors. They are often used in complex numeric computation, especially for the complex ones that involve a range of different values, such as for training machine learning algorithms or lengthy scientific calculations.
The Vector API in Java18 offers an easy approach for Java developers to easily perform vector computations in their applications. The vector API has also been incubated in earlier releases, specifically in JDK 16 and JDK 17. This API is designed to speed up the vector computations. It enables Java developers to perform vector calculations in lesser time and with relatively less effort than before. The vector API in Java18 aims to provide a way to write complex algorithms for calculations using the existing HotSpot auto-vectorizer in Java and a user model that can make the vectorization more predictable.
· Internet-Address Resolution SPI
The Internet-address resolution SPI (service-provider interface) featured in Java18 allows defining an SPI for host and name address resolution so that the new Inet.Address command can make use of other resolvers instead of the built-in resolver available on the platform. Before that, The Java.net.InetAddress API could be used to convert hostnames to Internet Protocol (IP) addresses. This API used the built-in resolver provided by the operating system.
This feature has introduced several classes in the java.net.spi that can be used for their different offerings:
- InetAddressResolver – an interface that defines the methods for forwarding and backward lookup operations.
- The InternetAddressResolver – also a generic interface used for resolving Internet address items.
- LookupPolicy – the objects of this particular class describe the characteristics of a resolution request.
- InetAddressResolverProvider – this abstract class defines the service to be located by java.util.ServiceLoader.
- InetAddressResolverProvider – Configuration is an interface that describes the built-in configuration for resolution operations of the platform.
- Lastly, The InternetAddressResolverProvider – an abstract class that must be extended to define a custom lookup mechanism.
Following abstract methods are used for that but they must be overridden before use:
The inclusion of this feature in Java18 aims toward the integration of new network protocols, customization of network interface, and testing in the future. This feature does not involve developing a new resolver for the JDK.
· Foreign Function and Memory API
Java offers a variety of libraries for uninterrupted access to the non-JVM systems, for instance, the JDBC Drivers can be used to connect to RDBMS. Java18 has introduced the foreign function and memory API which allows Java developers to interoperate their Java code and data outside of the Java runtime.
Developers can now easily invoke foreign functions and can safely access foreign memory using this newly introduced API. It works by allowing Java programs to call native libraries and process native data without JNI (Java Native Interface). It aims to replace JNI with a better Java development model for foreign functions and memory handling.
· Pattern Matching with Switch statements
Pattern matching is an old feature in the Java programming language. It simplifies the task of making an object against a specific pattern. For instance, you can easily check if an object is the instance of a particular type or not so it can be accessed in a type-safe manner. This feature significantly increases the safety of your Java code. The Java18 offers a feature to use Pattern matching in switch statements.
See Also: Top 14 In-demand Tech Jobs Of 2022
First previewed in JDK17, the second preview of pattern matching for the switch statement in Java18 would enhance the switch expressions and statements with pattern matching. Extending the pattern matching to switch statements will enable the Java coders to test an expression against multiple patterns. It will allow expressing complex data-oriented queries relatively safer than before.
Although JDK 18 is a non-LTS (Long Term Support) release, it still has a lot to offer. Java18 is certainly a significant update, with new features like setting UTF-8 by default, a simple web server along with enhancements such as pattern matching with switch case, code snippets in Java API documentation and internet address resolution SPI, it is quite evident that the introduction of such remarkable features in Java18 is an attempt towards making Java more intuitive and accessible. It would surely be enough to persuade developers from different backgrounds and skillsets to start using Java.