JDK13 has been out for almost 2 months now, with a few new features being tried out here and there, but not the whole thing. It’s a shame that you don’t even know what features of the JDK(Java developerkit) you’re using as a Java developer. To become a more valuable Java developer, keep up with the latest features of the JDK.
Here, too, is a list of new JDK features that are overdue
JDK13
All JDK features are presented and tracked in the JEP progress: openjdk.java.net/jeps/0
What are the features for each of the JDK versions of the corresponding JDK home page view: openjdk.java.net/projects/jd…
JDK13 has five main features:
- 350: Dynamic CDS Archives reference: www.jianshu.com/p/0b8a9d137…
- 351: ZGC: Uncommit Unused Memory reference: www.jianshu.com/p/18fc5a042…
- 353: Reimplement the Legacy Socket API
- 354: Switch Expressions (Preview)
- 355: Text Blocks (Preview)
Dynamic CDS Archives
A new feature introduced in JDK10, but the creation steps were cumbersome at the time.
Steps required in # JDK10-xx :DumpLoadedClassList=classes.lst 2. -xshare :dump -xx :SharedArchiveFile -xx :SharedClassListFile=classes.lst 3. Use archive -xshare: on-xx :SharedArchiveFileCopy the code
New option introduced in JDK13 to archive automatically upon application exit:
java -XX:ArchiveClassesAtExit=app.jsa -cp app.jar HelloDemo
Copy the code
The steps for using archiving are the same as before, with -xshare :on enabled by default
Class loading process:
Load -> Verify -> Prepare -> Parse -> Initialize -> Use -> Uninstall
- Load: find the Class location and read the contents of the Class file from the Class location
- Validation: file format validation, metadata validation, bytecode validation, and symbol reference validation.
- Preparation: The stage of formally allocating memory for class variables and setting their initial values, which will be allocated in the method area. Stored as JDK internal data structures
- Parsing: The process by which the virtual machine converts symbolic references in a constant pool into direct references, parsing interfaces, and field parsing
- Initialization: Creates a class
CDS is designed to speed up application startup. Class-data only needs to be created once and reused later, reducing loading, validation, and preparation. There may be a parsing phase
Reference: App CDS combat
ZGC: Uncommit Unused Memory
ZGC was introduced in JDK11 to guarantee shorter pauses for GC, up to 10ms, and in JDK13 to return uncommitted, unused memory to the operating system
The ZGC consists of a number of Zpages, which are memory regions of different sizes, divided into small, medium, and large. When ZGC compresses memory, Zpage is emptied into ZPageCache. ZPageCache is an area that is ready to be used. If it is used, it is immediately removed from ZPageCache to Zpage, but if Zpage in ZPageCache is not used for a long time, It becomes uncommitted memory and can be returned to the operating system later.
When ZGC compacts the heap, ZPages are freed up and inserted into a page cache, the ZPageCache.
# Set a time to remove Zpage from ZpageCache (evict)
-XX:+UnlockExperimentalVMOptions -XX:+ZUncommit -XX:ZUncommitDelay=<seconds>
Copy the code
Reference: ZGC complete Guide
Reimplement the Legacy Socket API
The underlying JDK implementation of Socket is very old, from JDK1.0 was used until now, the underlying Java and C code is very early, for JDK developers, very difficult to maintain and Debug, so re-implement the Socket API interface.
-
Prior to JDK13, use PlainSocketImpl
-
JDK13 introduces NioSocketImpl to replace PlainSocketImpl.
Here’s a HelloWorld example:
public class HelloApp {
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(8888)) {
boolean running = true;
System.out.println("listened 8888");
while (running) {
Socket clientSocket = serverSocket.accept();
//dosomething with clientSocket } } catch (IOException e) { e.printStackTrace(); }}}Copy the code
But we can still switch to PlainSocketImpl. Need to configure jdk.net.usePlainSocketImpl
/Library/Java/JavaVirtualMachines/jdk-13.jdk/Contents/Home/bin/java -XX:+TraceClassLoading me/aihe/HelloApp.java | grep -i socketI
Copy the code
/Library/Java/JavaVirtualMachines/jdk-13.jdk/Contents/Home/bin/java -XX:+TraceClassLoading -Djdk.net.usePlainSocketImpl me/aihe/HelloApp.java | grep -i socketI
Copy the code
Switch Expressions (Preview)
A new yield keyword was introduced to return the contents of switch statements. When we first wrote a switch statement, we did some initialization before the statement. Now we can directly get the result of the SWicth statement
The original switch:
int numLetters;
switch (day) {
case MONDAY:
case FRIDAY:
case SUNDAY:
numLetters = 6;
break;
case TUESDAY:
numLetters = 7;
break;
case THURSDAY:
case SATURDAY:
numLetters = 8;
break;
case WEDNESDAY:
numLetters = 9;
break;
default:
throw new IllegalStateException("Wat: " + day);
}
Copy the code
In JDK13 you can write:
# No logical return
int numLetters = switch (day) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9;
};
# Logic more processing
String result = switch (number) {
case1, 2: // the logical code yield"one or two";
case3: // The logical code yield"three";
case 4, 5, 6:
yield "four or five or six";
default:
yield "unknown";
};
return result;
Copy the code
Text Blocks (Preview)
When you start writing long strings, you often use multiple string concatenation, which is a waste of performance and looks ugly. Especially when writing HTML strings or SQL statements.
// For example, HTML String HTML ="<html>\n" +
" \n" +
" Hello, world
\n" +
" \n" +
"</html>\n";
Copy the code
It can now be written as:
String html = """ Hello, world
""";
Copy the code
Note:
- One slight difference is that the beginning of the “”” must be followed by another line, and the end of the “”” has a different effect
- Note that each line may need to be treated with white space
"""
line 1
line 2
line 3
"""= >"line 1\nline 2\nline 3\n"
Copy the code
"""
line 1
line 2
line 3"""= >"line 1\nline 2\nline 3"
Copy the code
The last
JDK13 can speed up our development to some extent… Most importantly, its archiving features can greatly reduce the startup time of our applications, while ZGC can be a boon when memory is tight.
It’s worth a try!