Preface
Java 18 is officially released, and although it is not a long-term support (LTS) version, it implements nine JEPs (inJava 18Listed). What are the characteristics worth paying attention to?
JEP 400
Specify UTF-8 as the default character set for the standard Java API. With this change, APIs that rely on the default character set will remain consistent across all implementations, operating systems, locales, and configurations.
JEP 408
There is finally a native web server in Java. But please note that it does not have CGI or Servlet-like features 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 and other products, nor is it not recommended to use in production environments. It simply provides a command line tool to help developers design, test, and teach.
JEP 413
Supports the use of code snippets in Java API documentation. In the past, it was very troublesome to write some examples in Java code comments, and even character escapes were required. Java annotations now introduce a new tag @snippet to solve the problem of including snippet samples in the annotation.
It can be used inline:
/** * The following code shows how to use {@code }: * {@snippet : * if (()) { * ("v: " + ()); * } * } */
You can also reference external fragments:
/** * The following code shows how to use {@code }: * {@snippet file="" region="example"} */
It is the source code it references:
public class ShowOptional { void show(Optional<String> v) { // @start region="example" if (()) { ("v: " + ()); } // @end } }
JEP 417
An API is introduced to express vector computing that can be reliably compiled at runtime into the best vector instructions on supported CPU architectures, achieving performance over equivalent scalar computing. It is currently the third incubation.
JEP 418
Define the Service Provider Interface (SPI) for hostname and address resolution so thatYou can use parsers other than the platform's built-in parsers. This provides an entry for access to some Internet protocols, and you can also make some improvements and customizations to existing solutions.
JEP 419
Foreign Function & Memory API ( JEP 419) is one of the more important JEPs implemented in this version, because it isProject PanamaOne of the incubation components included. Panama is simplifying the process of connecting Java programs to non-Java components. This special feature introduces an API in its second incubation iteration through which Java programs call the Native class library and process Native data. The purpose is to replace the very undesirable Java Native Interface (JNI) designed.
Everyone knows that other languages have some great class libraries, but Java wants to call other languages' class libraries currently need to use JNI. However, JNI is designed to be too complicated, making it difficult for many Java developers to get started. 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 real JEP that implements Java language is Pattern Matching for switch (JEP 420), it was previewed for the first time in Java 17 (this is the second time). Its purpose is to "enhance the Java programming language through pattern matching of switch expressions and statements and extensions of pattern languages. In Java 16,JEP 394Extended 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 ... }
After using instanceof, we can use its real type without having to type convert the object.
Java 14 introduces switch expressions again:
int numLetters = switch (day) { case MONDAY, FRIDAY, SUNDAY -> 6; case TUESDAY -> 7; case THURSDAY, SATURDAY -> 8; case WEDNESDAY -> 9; default -> 11; };
If these two can be combined and switch can match patterns, the following sentence will be greatly simplified:
static String formatter(Object o) { String formatted = "unknown"; if (o instanceof Integer i) { formatted = ("int %d", i); } else if (o instanceof Long l) { formatted = ("long %d", l); } else if (o instanceof Double d) { formatted = ("double %f", d); } else if (o instanceof String s) { formatted = ("String %s", s); } return formatted; }
The preview feature of JEP 420 will simplify the above verbose code to:
static String formatterPatternSwitch(Object o) { return switch (o) { case Integer i -> ("int %d", i); case Long l -> ("long %d", l); case Double d -> ("double %f", d); case String s -> ("String %s", s); default -> (); }; }
Is it clearer?
JEP 421
The Object object has a finalize method, which is used for operations that are triggered when an instance is recycled by the garbage collector. When GC (Garbage Collector) determines that there is no more reference to the object, the object's garbage collector calls this method. At the time it was designed to avoid memory leaks, and now there are better alternatives to try-with-resources and Java 9 introduced.
Therefore, all this method will be marked as outdated and will be removed in the future.
Summarize
Very few people use JDK 18 in production because it is not the LTS version. It is more important to release the JDK 17 LTS version in September last year. Many class libraries, especially Spring framework 6.0 and Spring Boot 3.0, will be based on JDK17. How long will you last in Java 8? There are already 10 versions different. The next one is LTS is Java 21 in September 2023.
The above is the detailed explanation of the key points of Java 8 plus Java10 equals Java18 version. For more information about Java8java10 equals Java18 version, please pay attention to my other related articles!