编程知识 cdmana.com

Java 17 new features overview

Java 17 Plans to 9 month 14 Promulgated by the , Versions from different vendors will be released on or after the same day .Java 17 What's special about ,Oracle and OpenJDK Community decision , This will be a long supported version , And Java 11 and Java 8 The previous version is the same .

since Java 10 Since the introduction of fast release rhythm , except Oracle Outside , Many vendors are stepping up production of producible binaries with different levels of support , Include Amazon、Azul、BellSoft、Microsoft、SAP and Eclipse Adoptium( It used to be OpenJDK).

Here are the new features :

macOS Upper AArch64 Support

A prominent feature added in this release is support for AARC64 Upper macOS,Apple New... Last year's CPU series (M1) Support . For people running on these platforms , This is not news , Because some suppliers have already released their comments on JDK edition Framework support , It can even be traced back to Java8. For all that , The officially approved version is still very important for the future maintenance and support of the platform . by comparison ,Java9 Added a pair of Linux/AArch64 Platform support ,Java16 Added a pair of Windows/AArch64 Platform support .

Sealing class

Sealed Classes Seal class this feature has completed the preview phase , Now it's Java 17 The standard part of the language and platform , As defined in JEP 409. Sealed classes allow developers to explicitly declare subtypes of types , This prevents others from inadvertently extending or implementing it .

Other new features

Java 17 It also applies to AWT/Swing Running on macOS, The application brings a new rendering pipeline (JEP 382), Use Apple Of Metal API Instead of OpenGL, And generating random numbers API And new ways to enhance (JEP 356).

Other restricted or deprecated features :

JDK Internal elements are strongly encapsulated

JEP403 Strong encapsulation JDK All internal elements of , But the key inside API With the exception of , Such as sun.misc.Unsafe. Can no longer access ... Through a single command line option . take JDK Convert from the default loose strong encapsulation to the default strong encapsulation .

keep Java9 state , If the user tries to use reflection or likes to bypass other internal API Normal limits , A runtime warning will be issued . Command line parameters have also been added to control this behavior . keep Java 16, The default setting will change from warning to deny access by throwing an exception , But leave the command-line parameters to change the behavior .

Now in Java17 in , These command line parameters are further deleted , Of course , This restriction can also be disabled , This means that for these internal API, All unauthorized access is now strongly encapsulated .

benefits :

  • Keep improving JDK Security and maintainability of
  • Encourage developers to move from using internal elements to using standards API, So that they and their users can easily upgrade to the future Java edition .

Always strict floating point semantics

Recovery is always strictly floating point semantics (JEP 306).Java 1.2 Introduced in Java Changes to the default floating point semantics , Essentially allow JVM Performance comes at the expense of a little floating-point precision . For methods and classes that need to apply strict semantics , The introduction of keywords strictfp. From then on , stay CPU Added a new instruction set , This produces strict floating point semantics , It doesn't cost too much , therefore , No longer have the motivation of default and strict semantics .

Java17 Deleted the previous default semantics , Now strictly perform all floating-point operations . keyword strictfp There is still , But it has no effect , And a compile time warning will be generated .

Delete experiment AOT and JIT compiler

be based on Java advance (AOT) And instant (JIT) Compiler deletion experiment . Since the introduction of the compiler , It's almost useless , It takes a lot of work to maintain it .

java9 introduce advance(AOT) Compiled into Graal compiler ( A kind of use Java Compilation of JIT compiler ) Experimental function of .java10 Use the added JVMCI The interface makes Graal The compiler can be used as OpenJDK Media JIT The compiler uses .

keep JEP 410 stay ,AOT and JIT The compiler has been removed .

Delete RMI Activate

RMI Activate retention JEP 407 Deleted in , Retain Java 8 Become optional in , Last in Java 15 Is deprecated and marked for deletion .RMI Activate a through RMI Method to activate distributed object on-demand resources , But it's almost useless , There are better alternatives .RMI The rest is not affected by the active part .

Delete Applet API

Applet api In the end in jep398 Is marked for deletion , It's in java9 Abandoned in .Applet API This paper provides a method to embed Java AWT/Swing Control method , But today , No modern browser supports this approach , So over the past decade or more ,Applet API Basically nothing to do with this .

Discard the Security Manager

without doubt , The biggest thing to give up is Security Manager(JEP411). Security manager from Java 1.0 It began to exist , It is usually designed to limit what can be performed on the local computer Java operation , for example , Restrict access to files 、 Access to the network, etc , Or try to disable reflection and some API To sandbox untrusted code .

Security Manager deprecation began with Java 12, It adds a command line parameter that prohibits its use , from Java 18 Start , This command-line parameter disables setting up the security manager at run time by default .Java 17 The changes in mean , When trying to set up the security manager dynamically from the command line or at run time ,JVM A runtime warning will be generated .

Incubator and preview functions

Many people want to know Java 17 Whether there will be incubator and preview function , Because it has been upgraded to a long-term supported version , It seems unwise to support a feature that has changed or hasn't been cancelled for a long time . But now we see ,Java17 There are two incubator modules and a preview language function

Vector API

Vector API(JEP 414) Entering the second incubator stage .API Enables developers to represent vector calculations , then JIT The compiler can compile it , To run CPU Of JVM And the appropriate vector instructions supported by the architecture ( for example , utilize SSE and AVX Instruction set ).

Before that , Developers either have to rely on scalar operations , Or you have to use / Develop platform specific native libraries . keep Java To implement vectors API It can also provide elegant fallback for things that do not have necessary instructions in the current architecture , contrary , You must return and calculate differently .

Although not this JEP Part of , But vectors API The standardization of also makes JDK Class can use it . such as Arrays.mismatch Things like that , today , There is an inherent vectorization implementation on some platforms , You can rewrite it , Make it all in Java Run-in Run in , So there's no need to be in JVM Write and maintain multiple platform specific implementations in .

External functions and memory API

Another incubator module is external functions and memory API(JEP 412), Technically speaking , It is Java 16 Merged and developed the previous two incubator modules : External linker API(JEP 389) And external memory access API(JEP 393). These two combinations allow the use of Java Write statically typed code to access native code and memory , The purpose is to be able to ... under these circumstances ​​ JNI The third generation used .

Switch Pattern matching

Java 17 The last language preview function in is used to switch pattern matching (JEP 406). This language extends Switch Expressions and statements , Make it also ... Switch based on type , Similar to pattern matching instanceof(jep394) The introduction of grammar , The syntax is in java16 Standardized in .

Before that , If you want to perform different operations according to the dynamic type of the object , You need to use instanceof Check establish If-else-If chain , for example :

String type(Object o) {  if (o instanceof List) {    return "A List of things.";  }  else if (o instanceof Map) {    return "A Map! It has keys and values.";  }  else if (o instanceof String) {    return "This is a string.";  }  else {    return "This is something else.";  }}

Use a combination of switch Expression and switch New pattern matching , It can be simplified as :

String type(Object o) {  return switch (o) {    case List l -> "A List of things.";    case Map m -> "A Map! It has keys and values.";    case String s -> "This is a string.";    default -> "This is something else.";  };}

Original address :https://javamana.com/2021/09/20210914020026062u.html

版权声明
本文为[Old K's Java blog]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/10/20211002145756166q.html

Scroll to Top