likes look again, the power is unlimited. Hello world search "161169a07696a8 161169a07696a9 program monkey Alang ".
This article Github.com/niumoo/JavaNotes and unread code blog has been included, there are many knowledge points and series of articles.
This article belongs to the Java new feature tutorial series, which has been included in Github.com/niumoo/JavaNotes , please like it, don’t get lost.
Since Oracle adjusted the release rhythm of the Java version, the Java version has been released faster and faster. Although it is said that the Java version is released, I use Java 8, but the new version of the Java function still needs to be learned.
Java 13 was released as early as September 2019. Although it is not a long-term support version, it also brings many new features.
Java 13 official download: https://jdk.java.net/archive/
Java 13 official document: http://openjdk.java.net/projects/jdk/13/
Java 13 new features :
- JEP 350: Dynamic CDS Archive
- JEP 351: ZGC, return unused memory (experimental)
- JEP 353:
- JEP 354: Switch Expression (Secondary Preview)
- JEP 355: Text Block (Preview)
<!-- more -->
Extension: This article belongs to the Java new feature tutorial , which will introduce the new features of each version of Java, you can click to browse.
1. JEP 350 Dynamic CDS Archive
One step when JVM starts is to load classes in memory, and if there are multiple jars, the speed of loading the first jar is the slowest. This prolongs the startup time of the program. In order to reduce this time, Java 10
introduced the application class data sharing (CDS) mechanism, which can share the classes you want to share between programs, so that different Java processes can share this Class to reduce the space occupied by this class and loading speed. However, the steps to use this feature in Java 10 are cumbersome.
Extended reading: Java 10 New Features Introduction
The AppCDS in Java 13 allows Java applications to be dynamically archived at the end of program execution (if the JVM does not crash); the stored content includes all loaded application type classes and used class libraries. These stored class libraries do not exist. In the default CDS archive.
Using this function is very simple, only need to increase the startup parameters when the program starts.
# ArchiveClassesAtExit,程序结束时动态存档
bin/java -XX:ArchiveClassesAtExit=hello.jsa -cp hello.jar Hello
# SharedArchiveFile,使用指定存档启动
bin/java -XX:SharedArchiveFile=hello.jsa -cp hello.jar Hello
2. JEP 351: ZGC, return unused memory (experimental)
Before Java 13, although ZGC caused very little pause time when cleaning up the memory, even if the memory has not been used for a long time, ZGC would not return the memory to the operating system, which is very unfavorable for those applications that are very concerned about memory usage. friendly.
for example:
- A container environment on the cloud where resources are paid for by usage.
- Although the application has been idle for a long time, it occupies the memory, causing the memory of other running programs to be tight.
The newly added function allows ZGC to return the memory that has not been used for a long time to the operating system, which is very friendly to some users.
3. JEP 353: Reimplement the Socket API
java.net.Socket
and java.net.ServerSocket
classes have been introduced as early as Java 1.0. The Java code and C language code of their implementation are mixed, and maintenance and debugging are very difficult; and this implementation still has concurrency problems, and sometimes it is difficult to troubleshoot.
Therefore, a new implementation was introduced in Java 13, using the new implementation NioSocketImpl
to replace the old PlainSocketImpl
implementation. Although the functions are the same, the old method will not be deleted in the current and future versions. Users can switch back to the old implementation method -Djdk.net.usePlainSocketImpl
Write a test class to find this change.
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Test {
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(8000)){
boolean running = true;
while(running){
Socket clientSocket = serverSocket.accept();
//do something with clientSocket
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Use Java 13 to run, track the loaded classes -XX:+TraceClassLoading
NioSocketImpl
log.
➜ develop ./jdk-13.0.2.jdk/Contents/Home/bin/java -XX:+TraceClassLoading Test.java | grep SocketImpl
[0.699s][info ][class,load] java.net.SocketImpl source: jrt:/java.base
[0.699s][info ][class,load] java.net.SocketImpl$$Lambda$173/0x0000000800c37440 source: java.net.SocketImpl
[0.702s][info ][class,load] sun.net.PlatformSocketImpl source: jrt:/java.base
[0.702s][info ][class,load] sun.nio.ch.NioSocketImpl source: jrt:/java.base
[0.713s][info ][class,load] sun.nio.ch.NioSocketImpl$FileDescriptorCloser source: jrt:/java.base
But in Java 12 it is not NioSocketImpl
.
➜ develop ./jdk-12.0.2.jdk/Contents/Home/bin/java -XX:+TraceClassLoading Test.java | grep SocketImpl
[0.665s][info ][class,load] java.net.SocketImpl source: jrt:/java.base
[0.665s][info ][class,load] java.net.AbstractPlainSocketImpl source: jrt:/java.base
[0.665s][info ][class,load] java.net.PlainSocketImpl source: jrt:/java.base
[0.665s][info ][class,load] java.net.SocksSocketImpl source: jrt:/java.base
[0.666s][info ][class,load] java.net.AbstractPlainSocketImpl$1 source: jrt:/java.base
4. JEP 354: Switch Expression (Second Preview)
Why are you reluctant to use the expression switch
I think one of the reasons should be that switch
expression is not beautiful or elegant, or even verbose. For example, like the following example.
package com.wdbyte;
public class Java13Switch {
public static void main(String[] args) {
//通过传入月份,输出月份所属的季节
System.out.println(switchJava12Before("march"));
}
public static String switchJava12Before(String month) {
String reuslt = null;
switch (month) {
case "march":
case "april":
case "may":
reuslt = "春天";
break;
case "june":
case "july":
case "august":
reuslt = "夏天";
break;
case "september":
case "october":
case "november":
reuslt = "秋天";
break;
case "december":
case "january":
case "february":
reuslt = "冬天";
break;
}
return reuslt;
}
}
In Java 12, switch
has been improved so that it can cast L ->
expressions and can have return values, so the code is more beautiful to use, but this is a preview feature in Java 12.
// 通过传入月份,输出月份所属的季节
public static String switchJava12(String month) {
return switch (month) {
case "march", "april", "may" -> "春天";
case "june", "july", "august" -> "夏天";
case "september", "october", "november" -> "秋天";
case "december", "january", "february" -> "冬天";
default -> "month erro";
};
}
Extended reading: Java 12 new feature introduction , JEP 325: Switch expression
Now, in Java 13, the switch
expression has been enhanced, and the yield
keyword is added to return the value. Compared with break
, the semantics are more clear.
public static String switchJava13(String month) {
return switch (month) {
case "march", "april", "may":
yield "春天";
case "june", "july", "august":
yield "夏天";
case "september", "october", "november":
yield "秋天";
case "december", "january", "february":
yield "冬天";
default:
yield "month error";
};
}
5. JEP 355: Text Block (Preview)
Before that, if we assign a JSON to a string:
String content = "{\n"
+ " \"upperSummary\": null,\n"
+ " \"sensitiveTypeList\": null,\n"
+ " \"gmtModified\": \"2011-08-05 10:50:09\",\n"
+ " \"lowerGraph\": null,\n"
+ " \"signature\": \"\",\n"
+ " \"appName\": \"xxx\",\n"
+ " \"lowerSummary\": null,\n"
+ " \"gmtCreate\": \"2011-08-05 10:50:09\",\n"
+ " \"type\": \"CALL\",\n"
+ " \"name\": \"xxxx\",\n"
+ " \"subType\": \"yyy\",\n"
+ " \"id\": 1,\n"
+ " \"projectId\": 1,\n"
+ " \"status\": 1\n"
+ "}";
Finally, there is no need to write ugly long strings. Starting from Java 13, you can define strings in text blocks.
String content2 = """
{
"upperSummary": null,
"sensitiveTypeList": null,
"gmtModified": "2011-08-05 10:50:09",
"lowerGraph": null,
"signature": "",
"appName": "xxx",
"lowerSummary": null,
"gmtCreate": "2011-08-05 10:50:09",
"type": "CALL",
"name": "xxxx",
"subType": "yyy",
"id": 1,
"projectId": 1,
"status": 1
}
""";
But this is a preview function. If you want to use it in Java 13, you need to manually turn on the preview function. This function is officially released in Java 15.
If you want to try, you can go to download the latest version of , and have fun.
refer to
- http://openjdk.java.net/projects/jdk/13/
- https://nipafx.dev/java-application-class-data-sharing/
- https://www.wdbyte.com/2020/02/jdk/jdk12-feature/
- https://mkyong.com/java/what-is-new-in-java-13/
- Java New Features Tutorial
Related Reading
- Java 12 New Features Introduction
- Java 11 New Features Introduction
- Introduction to the new features of Java 10
- Java 9 new feature introduction
- Java 8 function interface UnaryOperator
- Java 8 functional interface BiPredicate
- Java 8 function interface BiFunction
- Java 8 functional interface Supplier
- Java 8 function interface Predicate
- Java 8 function interface Function
- Java 8 new features-forEach traversal
- New Features of Java 8-LocalDate, LocalDateTime Time Processing Introduction
- Java 8 New Features-Use Optional to gracefully handle null pointers
- New Features of Java 8-Lambda Expressions, Function Interfaces Learn about
- New Features of Java 8-Super Stream Streaming Operation Posture, Don’t Learn
- New Features of Java 7-Say goodbye to inefficient IO, Introduction to File, Paths, and Path file operations
- Java 7 New Features-New Features-Come and add a wave of Java 7 syntax features
<end>
Hello world:) I am Aran, a first-line technical tool person, and write articles seriously.
likes . All of them are talents. Not only are they handsome and good-looking, they are also good-sounding.
The article is continuously updated. You can follow the public program Alang " or visit " Unread Code Blog ".
Reply [Information] There are various series of knowledge points and must-read books I prepared.
This article Github.com/niumoo/JavaNotes has been included, there are many knowledge points and series of articles, welcome to Star.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。