Inspecting Features In Java 18

Introduction

The article introduces the new features slated to release in Java 18. Every new release in Java has a preview feature which will release as a feature in further Java releases. For example, Pattern Matching is a Preview feature in Java 17 to explore this feature one must supply “–enable-preview” as a parameter.

If something is an “incubator module” meaning it’s not a permanent feature yet, it is released with the intention to get feedback from developers, such features may be modified or completely removed depending on the feedback. For exploring this, one must supply “—add-modules” as a parameter.

JEP 400: UTF-8 by Default

From Java 18 UTF-8 will be the default charset across the platform, with the default charset the applications will behave consistently across all Operating systems, locales. If you run your application on windows.

public static void main(String[] args) {
		System.out.println(System.getProperty("file.encoding"));
}

returns # Cp1252

Whereas if we run on another OS then it may return UTF-8, but we can always enforce the default encoding

public static void main(String[] args) {
		System.setProperty("file.encoding", "UTF-8");
		System.out.println(System.getProperty("file.encoding"));
}

returns UTF-8

Java 18 onwards, UTF-8 will be the default charset.

JEP 408: Simple Web Server

Java 18 has a minimal web server that will serve the static files, it’s a command-line tool. This tool will be helpful for prototyping, testing purposes.

JEP 420: Pattern Matching for a switch (Second Preview)

The Pattern matching as of Java 17 is a preview feature, it will be in a second preview state from Java 18. I am assuming in the Second Preview it will start throwing compilation errors if all the Sealed sub-classes are not included in the pattern matching clause.

@SuppressWarnings("preview")
public static String patternMatch(Account obj) {
    return switch (obj) {
        case SavingsAccount sa - > "Savings Account";
        case CheckingAccount ca - > "Checking Account";
        case MoneyMarketDepositAccount mmd - > "Money Market Deposit Account";
        default - > "Not Found";
    };
}

JEP 421: Deprecate Finalization for Removal

Finalization will be deprecated from Java soon, it will still be enabled but can be completely disabled with the command-line option –finalization=disabled, the command line option will help in facilitating the early testing, in a future release it will be disabled by default. This also includes the “finalize()” method and “finally” clause with try-catch-finally, one should not use finally to release resources instead try-with-resources should be preferred.

JEP 416: Reimplement Core Reflection with Method Handles

This JEP is an implementation change of the Reflection API; it has no impact on the consumers of Reflection API. The summary of implementation is “java.lang.reflect.Method, Constructor, and Field on top of java.lang.invoke method handles. Making a method handles the underlying mechanism for reflection will reduce the maintenance and development cost of both the java.lang.reflect and java.lang.invoke APIs”.

JEP 418:  Internet-Address Resolution SPI

As the title suggests this proposal defines an SPI (service provider interface) which is used for resolving hostname and address resolution so that java.net.InetAddress API should use SPI rather than using the Operating system domain name resolver.  The java.net.spi package has the following classes

  • InetAddressResolverProvider 
  • InetAddressResolver 
  • InetAddressResolver.LookupPolicy 
  • InetAddressResolverProvider.Configuration 

JEP 417: Vector API (Third Incubator)

The incremental development of Vector API which is introduced in the version Java 16 (JEP 338) and Java 17 (JEP 414).

JEP 419: Foreign Function & Memory API (Second Incubator)

The incremental (second iteration) of the API.

Summary

The article explains the high-level changes and features coming in Java 18.


Similar Articles