In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "what are the new features of Java12 to 17", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what are the new features of Java12 to 17" article.
Switch expression
Switch can now return a value, just like an expression:
/ / assign the group of a given planet to a variable String group = switch (planet) {case MERCURY, VENUS, EARTH, MARS-> "inner planet"; case JUPITER, SATURN, URANUS, NEPTUNE-> "outer planet";}
If you need more code on the right side of a single case, you can write it to the block and return the value using the following method of yield:
/ print the group of a given planet, and / / and assign the group of a given planet to a variable String group = switch (planet) {case EARTH, MARS-> {System.out.println ("inner planet"); System.out.println ("mainly rocky"); yield "internal";} case JUPITER, SATURN-> {System.out.println ("outer planet") System.out.println ("mainly composed of gases"); yield "external";}}
However, switching with the new arrow tag does not require a return value, just like the void expression:
/ print the group// of a given planet does not return anything switch (planet) {case EARTH, MARS-> System.out.println ("inner planet"); case JUPITER, SATURN-> System.out.println ("outer planet");}
Compared with the traditional switch, the new switch expression
Use "- >" instead of ":"
Multiple constants are allowed per case
There is no semantic penetration (that is, no interruption is required)
Localize variables defined in the case branch to this branch
In addition, the compiler ensures the completeness of the switch because only one case is executed, which means either
All possible values are listed as a case (for example, the enumeration above consists of eight planets)
Default branch must be provided
Text block
Text blocks allow you to write multiline strings containing double quotes without using\ n or\ "escape sequences:
String block = "" can enter multiple lines of text, can be indented, can be marked with "double quotes"! ""
The text block is opened by three double quotes "" and a newline character and closed by three double quotes.
The Java compiler uses an intelligent algorithm to remove leading spaces from the result string, making
Removed indentation only related to better readability of Java source code
The indentation associated with the string itself remains the same
In the above example, the result string is as follows, each of which is. Mark a space:
.. You can enter multiple lines of text. It can be indented. Can be marked with "double quotation marks"!
Imagine a vertical bar that spans the height of a block of text, moving from left to right and removing spaces until it touches the first non-white space character. It is also important to end the text block delimiter, so move it two positions to the left.
String block = "" can enter multiple lines of text, can be indented, can be marked with "double quotes"! ""
The result is in the following string:
You can enter multiple lines of text.. It can be indented. Can be marked with "double quotation marks"!
In addition, the trailing space of each line is removed, which can be prevented by using a new escape sequence.
Newline characters within a text block can be escaped:
String block = "" Please\ do not\ jump the queue\,\ Thank you\ ""
As a result, there are no newline characters in the following string:
Please。 No。 Cut in line. Thank you
Alternatively, you can delete the last newline character by appending the end delimiter directly to the end of the string
String block = "" there is no final break at the end of the string ""
Inserting a variable into a text block can be done as usual using the static method String::format or the new instance method String::formatted, which is shorter to write:
String block = ""% s tag location. "" .formatted ("x"); packaging tool
Suppose you demo.jar has a JAR file in the lib directory, along with other dependencies JAR. The following command
Jpackage-name demo-input lib-main-jar demo.jar-main-class demo.Main
Package this demo application into a native format that corresponds to your current platform:
Linux:deb or rpm
Windows:msi or exe
MacOS:pkg or dmg
The generated package also contains the JDK section needed to run the application and the native initiator. This means that users can install, run, and uninstall applications in a standard platform-specific manner without explicitly installing Java in advance.
Cross-compilation is not supported: if a Windows user's package is needed, it must be created with jpackage on the Windows machine.
You can customize package creation with more options, which are documented on the jpackage man page.
Pattern matching of Instanceof
Pattern matching instanceof eliminates boilerplate code that performs casting after type comparison:
Object o = "string disguised as object"; if (o instanceof String s) {System.out.println (s.toUppperCase ());}
In the above example, the scope of the new variable s is intuitively limited to the if branch. To be exact, variables are within the scope of ensuring pattern matching, which also makes the following code valid:
If (o instanceof String s &! s.isEmpty ()) {System.out.println (s.toUpperCase ());}
And vice versa
If (! (o instanceof String s)) {throw new RuntimeException ("excepting string");} / / s is in this range! System.out.println (s.toUpperCase ()); record
Recording reduces the boilerplate code for classes that act as simple data carriers:
Record Point (int x, int y) {}
This single line produces an automatically defined record class
Fields of x and y (private and final)
Canonical constructor for all fields
Getters in all areas
Equals, hashCode, and toString (consider all fields)
/ / canonical constructor Point p = new Point (1,2); / / getters-does not have the 'get' prefix p.x (); p.y (); / / equals, hashCode, toStringp.equals (new Point (1,2)); / / truep.hashCode (); / / values dependent on x and y p.toString (); / / Point
Some of the most important limitations of record classes are that they
Are immutable (because their fields are private and final)
Is implicitly final.
Unable to define other instance fields
Always extend the Record class
However, it is also possible:
Define other methods
Implementation interface
Custom specification constructor and accessor
Record Point (int x, int y) {/ / display specification constructor Point {/ / Custom validation if (x < 0 | | y < 0) throw new IllegalArgumentException ("no negative points allowed"); / / Custom adjustment (usually counterintuitive) x + = 1000; y + = 1000 / / the assignment of a pair of fields will automatically display the accessor public int x () {/ / custom code return this.x;}} at the end.
In addition, you can define local records in the method:
Public void withLocalRecord () {record Point (int x, int y) {}; Point p = new Point (1,2);} sealed class
The sealed class explicitly lists the allowed direct subclasses. Other classes may not be extended from this class:
Public sealed class Parent permits ChildA, ChildB, ChildC {...}
Similarly, the sealed interface explicitly lists the allowed direct subinterfaces and implementation classes:
Sealed interface Parent permits ChildA, ChildB, ChildC {...}
The class or interface permits in the list must be in the same package (if the parent module is in a named module, in the same module).
If the subclass (or interface) of the permits is in the same file, the list can be omitted:
Public sealed class Parent {final class Child1 extends Parent {} final class Child2 extends Parent {} final class Child3 extends Parent {}}
Each subclass or interface in the permits list must use one of the following modifiers:
Final (no further extension is allowed; only for subclasses, because the interface cannot be final)
Sealing (allows for further, limited expansion)
Unsealed (allows unlimited expansion again)
The above is the content of this article on "what are the new features of Java12 to 17". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.