Tomorrow Java 18 will be officially released, and while it is not a Long Term Support (LTS) release, it implements nine JEPs (listed at Java 18 ). What features are worth paying attention to? Today, the fat brother will interpret it for you in advance. Watch, like, retweet, and follow again.
JEP 400
Specifies UTF-8 as the default character set for the standard Java API. With this change, APIs that rely on the default charset will be consistent across all implementations, operating systems, locales, and configurations.
JEP 408
Java finally has a native Web server inside. But note that it doesn't have CGI or similar Servlet functionality available. The tool can be used for prototyping, ad hoc coding and testing purposes, especially in educational settings.
It is not a competitor of Jetty , Apache Tomcat and other products, and cannot and is not recommended for use in production environments. Just provide a command-line tool to assist developers in designing, testing, and teaching.
JEP 413
Support for code snippets in Java API documentation. In the past, it was very troublesome to write some samples in the comments of Java code, and even character escaping was required. Java annotations now introduce a new tag @snippet
to address the issue of including code snippet samples in annotations.
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());
* }
* }
*/
It is also possible to reference external fragments:
/**
* The following code shows how to use {@code Optional.isPresent}:
* {@snippet file="ShowOptional.java" region="example"}
*/
ShowOptional.java
is the source code it references:
public class ShowOptional {
void show(Optional<String> v) {
// @start region="example"
if (v.isPresent()) {
System.out.println("v: " + v.get());
}
// @end
}
}
JEP 417
Introduces an API to express vector computations that can be reliably compiled at runtime to the best vector instructions on supported CPU architectures, resulting in better performance than equivalent scalar computations. It is currently the third hatching.
JEP 418
Define a Service Provider Interface (SPI) for hostname and address resolution so that java.net.InetAddress
can use resolvers other than the platform's built-in resolvers. This provides access to some protocols of the Internet, 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 release, as it is one of the incubating components included in Project Panama . Panama is simplifying the process of connecting Java programs to non- Java components. This particular feature introduced a API , Java program in its second incubation iteration through this API to call the Native class library and process Native data. The intention is to replace the very suboptimal Java Native Interface (JNI) was designed.
Everyone knows that there are some great libraries in other languages, but Java To call libraries in other languages, you currently need to use JNI . But JNI is designed to be too complicated, making it difficult for many Java developers to get started. If this situation were to change, it would 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 affects the Java language is Pattern Matching for switch ( JEP 420 ), which was first previewed in Java 17 (this is the second preview). Its purpose is to "enhance the Java programming language by pattern matching for switch expressions and statements, and extensions to the pattern language. In Java 16, JEP 394 extends the instanceof
operator to take the operator to take the pattern of pattern matches :
// Old code
if (o instanceof String) {
String s = (String)o;
... use s ...
}
// New code
if (o instanceof String s) {
... use s ...
}
We use instanceof
to use its real type without needing to cast the object.
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;
};
If these two could be combined and switch
could be pattern matched, 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;
}
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();
};
}
Is it clearer?
JEP 421
Object
object has a finalize
method, which is used for operations that are triggered when the instance is reclaimed by the garbage collector. The object's garbage collector calls this method when the GC (garbage collector) determines that there are no more references to the object. At the time it was designed to avoid memory leaks, there are now better alternatives try-with-resources
and introduced by Java 9 java.lang.ref.Cleaner
.
Therefore, all this method will be marked as obsolete and will be removed in the future.
Summarize
Few people are using JDK 18 in production because it is not LTS release. Last September release JDK 17 LTS version is more important, a lot of libraries, especially Spring Framework 6.0 and Spring the Boot 3.0 will be based JDK17 , you have the the Java 8 persist for how long? There are 10 different versions. Next is LTS is Java 21 for September 2023.
Follow the public account: Felordcn for more information
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。