Java 18 will be officially released tomorrow, and while it’s not a long Term support (LTS) version, it does implement nine Jeps (listed in Java 18). What are the features to watch? Today fat brother for you to read in advance. Look again, like, retweet and follow.

JEP 400

Specify UTF-8 as the default character set for the standard Java API. With this change, apis that depend on the default character set will be consistent across all implementations, operating systems, locales, and configurations.

JEP 408

There’s finally a native Web server inside Java. Note, however, that there is no CGI or servlet-like functionality available. The tool can be used for prototyping, temporary coding, and testing purposes, especially in educational Settings.

It is not a competitor to Jetty, Apache Tomcat, etc., and cannot and is not recommended for use in production environments. It simply provides a command line tool to assist developers in designing, testing, and teaching.

JEP 413

Support for code snippets in Java API documents. In the past, it was very troublesome to write some samples in comments of Java code, and even to do character escape. Java annotations now introduce a new tag, @Snippet, to address the problem of annotations containing snippet samples.

It can be used inline:

/**
 * The following code shows how to use {@code Optional.isPresent}:
 * {@snippet: * if (v.isPresent()) { * System.out.println("v: " + v.get()); *} *} */

Copy the code

External fragments can also be referenced:

/**
 * The following code shows how to use {@code Optional.isPresent}:
 * {@snippet file="ShowOptional.java" region="example"}
 */
Copy the code

Showoption. Java is the source it references:

public class ShowOptional {
    void show(Optional<String> v) {
        // @start region="example"
        if (v.isPresent()) {
            System.out.println("v: " + v.get());
        }
        // @end}}Copy the code

JEP 417

An API is introduced to express vector computations that can reliably compile at run time to the best vector instructions on a supported CPU architecture, thus achieving better performance than equivalent scalar computations. This is the third incubation.

JEP 418

Define the Service provider interface (SPI) for hostname and address resolution so that java.net.InetAddress can use a parser other than the platform’s built-in parser. This provides access to some of the protocols on the Internet, and allows you to modify and customize existing solutions.

JEP 419

The Foreign Function & Memory API (JEP 419) is one of the more important Jeps implemented in this release, as it is one of the incubation components included in Project Panama. Panama is making it easier to connect Java programs to non-Java components. This special feature introduces an API in its second incubation iteration through which Java programs call Native libraries and process Native data. The goal is to replace the very poorly designed Java Native Interface (JNI).

We all know that other languages have great libraries, but Java needs to use JNI to call libraries in other languages. But JNI was designed to be too complex for many Java developers to learn. If this situation changes, it will be very easy to use Java to call some C or C++ audio and video processing libraries and Python machine learning libraries.

JEP 420

The only JEP implemented that really impacted the Java language was Pattern Matching for Switch (JEP 420), which was first previewed in Java 17 (this is the second preview). The purpose is to “enhance the Java programming language through pattern matching of switch expressions and statements and extensions to the pattern language. In Java 16, JEP 394 extends the instanceof operator to take type patterns and perform pattern matching:

// Old code if (o instanceof String) { String s = (String)o; . use s ... } // New code if (o instanceof String s) { ... use s ... }Copy the code

Using Instanceof allows us to use the real type of an object without having to cast it.

Java 14 introduced the switch expression:

int numLetters = switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> 6;
    case TUESDAY                -> 7;
    case THURSDAY, SATURDAY     -> 8;
    case WEDNESDAY              -> 9;
    default                     -> 11;    
};
Copy the code

If the two could be combined and the switch could do pattern matching, the following sentence would be greatly simplified:

static String formatter(Object o) {
    String formatted = "unknown";
    if (o instanceof Integer i) {
        formatted = String.format("int %d", i);
    } else if (o instanceof Long l) {
        formatted = String.format("long %d", l);
    } else if (o instanceof Double d) {
        formatted = String.format("double %f", d);
    } else if (o instanceof String s) {
        formatted = String.format("String %s", s);
    }
    return formatted;
}
Copy the code

The preview feature of JEP 420 will simplify the above verbose code to:

static String formatterPatternSwitch(Object o) {
    return switch (o) {
        case Integer i -> String.format("int %d", i);
        case Long l    -> String.format("long %d", l);
        case Double d  -> String.format("double %f", d);
        case String s  -> String.format("String %s", s);
        default        -> o.toString();
    };
}
Copy the code

Is it clearer?

JEP 421

Object objects have a Finalize method, which is used to instantiate the actions that are triggered when the garbage collector collects them. The object’s garbage collector calls this method when the GC (garbage collector) determines that there are no more references to the object. It was designed to avoid memory leaks at the time, and there are now better alternatives such as try-with-Resources and the java.lang.ref.cleaner introduced in Java 9.

Therefore, all methods are marked as obsolete and will be removed in the future.

conclusion

Few people use JDK 18 in production because it is not an LTS version. The LTS release of JDK17 last September is even more important. Many of the libraries, especially Spring Framework 6.0 and Spring Boot 3.0, will be based on JDK17. How long will you stick with Java 8? It’s already out of 10 versions. The next LTS is Java 21 in September 2023.

Follow our public id: Felordcn for more information

Personal blog: https://felord.cn