Java 13 released tomorrow, interpretation of the latest and most new features

August 2017, JCP Executive Committee of the Java release frequency to once every six months, the new release cycle strictly follow the point of time, will be released in September and March of each year.

Currently, JDK official website can already see progress JDK 13, the latest version of the JDK 13 will be released on September 17, 2019.

Currently, JDK13 in Release-Candidate Phase (release candidate stage), will be released September 17. Currently included in this release have all been fixed, mainly includes the following five:

JEP 350,Dynamic CDS Archives

JEP 351,ZGC: Uncommit Unused Memory

JEP 353,Reimplement the Legacy Socket API

JEP 354: Switch Expressions (Preview)

JEP 355,Text Blocks (Preview)

Let's introduce one by one these five important features.

Dynamic CDS Archives

This feature is in JEP310: Application Class-Data Sharing on the basis of the expansion comes, Dynamic CDS Archives of CDS refers to Class-Data Sharing.

So, this JEP310 is Gesha things?

We know that when you start multiple JVM on the same physical machine / virtual machine, each virtual machine to load all the classes they need a separate, memory usage and startup costs are relatively high. So Java team introduced the concept of CDS by some core classes shared between each JVM, each JVM only need to load your own application classes, start time is reduced, the other core classes is shared, so the JVM memory footprint decreased.

CDS can only act on the Boot Class Loader loaded class can not act on the App Class Loader Class Loader or self-defined classes loaded.

In Java 10, the CDS will be extended to AppCDS, as the name implies, AppCDS more than capable of acting on the Boot Class Loader, App Class Loader and Custom Class Loader also be able to work, greatly increased the scope of application of the CDS. Also it said that the development of custom classes can also be loaded to multiple JVM shared.

Java 10 contained JEP310 share common metadata by category across different Java processes to reduce the memory footprint and improves startup time.

However, JEP310 using AppCDS process is quite complex and requires three steps:

1、决定要 Dump 哪些 Class
2、将类的内存 Dump 到归档文件中
3、使用 Dump 出来的归档文件加快应用启动速度
复制代码

This time the JDK in 13 JEP 350, on the basis of JEP310, but also made some extensions. Allows a Java application has finished executing dynamic archiving class, class archive will include all application classes and class libraries loaded default base layer CDS (class data-sharing) archive does not exist.

In other words, re-use AppCDS in Java 13 in time, do not need so complicated.

ZGC: Uncommit Unused Memory

Before discussing this issue, would like to ask a question, JVM's GC memory will be released back to the operating system?

How memory after GC disposal, in fact, depending on the garbage collector. Because the memory back to the OS, meant to adjust the heap size of the JVM, this process is relatively resource-consuming.

In JDK 11 in, Java introduced ZGC, this is a scalable, low latency garbage collector, but was only experimental. And, ZGC freed memory is not returned to the operating system.

In the Java 13, JEP 351 ZGC do again enhanced this ZGC can not use heap memory is returned to the operating system. The reason why the introduction of this feature, because now there are many scenes in memory is relatively expensive resource, in the following cases, the memory back to the operating system is still very necessary:

  • 1, according to those who need to pay for the use of container
  • 2, the application may be idle for a long time and shared with many other applications competing for resources or environment.
  • 3, during the execution of the application may have very different heap space requirements. For example, the required period starting stack may be larger than later required during execution stack at steady state.

Reimplement the Legacy Socket API

Use easy to maintain and debug simpler and more modern implementations replace java.net.Socket and java.net.ServerSocket API.

java.net.Socket and achieve java.net.ServerSocket very old, the JEP is they introduce a modern implementations. Modern default Java implementation is achieved in the 13, but older implementations are not deleted, you can use them by setting the system property jdk.net.usePlainSocketImpl.

Socket and run one instance of the class ServerSocket displays the debugging output. This is the default (new):

java -XX:+TraceClassLoading JEP353  | grep Socket
[0.033s][info   ][class,load] java.net.Socket source: jrt:/java.base
[0.035s][info   ][class,load] java.net.SocketOptions source: jrt:/java.base
[0.035s][info   ][class,load] java.net.SocketImpl source: jrt:/java.base
[0.039s][info   ][class,load] java.net.SocketImpl$$Lambda$1/0x0000000800b50840 source: java.net.SocketImpl
[0.042s][info   ][class,load] sun.net.PlatformSocketImpl source: jrt:/java.base
[0.042s][info   ][class,load] sun.nio.ch.NioSocketImpl source: jrt:/java.base
[0.043s][info   ][class,load] sun.nio.ch.SocketDispatcher source: jrt:/java.base
[0.044s][info   ][class,load] java.net.DelegatingSocketImpl source: jrt:/java.base
[0.044s][info   ][class,load] java.net.SocksSocketImpl source: jrt:/java.base
[0.044s][info   ][class,load] java.net.ServerSocket source: jrt:/java.base
[0.045s][info   ][class,load] jdk.internal.access.JavaNetSocketAccess source: jrt:/java.base
[0.045s][info   ][class,load] java.net.ServerSocket$1 source: jrt:/java.base
复制代码

sun.nio.ch.NioSocketImpl output above is the realization of the new offer.

If you are using older implementations are also possible (specify the parameters jdk.net.usePlainSocketImpl):

$ java -Djdk.net.usePlainSocketImpl -XX:+TraceClassLoading JEP353  | grep Socket
[0.037s][info   ][class,load] java.net.Socket source: jrt:/java.base
[0.039s][info   ][class,load] java.net.SocketOptions source: jrt:/java.base
[0.039s][info   ][class,load] java.net.SocketImpl source: jrt:/java.base
[0.043s][info   ][class,load] java.net.SocketImpl$$Lambda$1/0x0000000800b50840 source: java.net.SocketImpl
[0.046s][info   ][class,load] sun.net.PlatformSocketImpl source: jrt:/java.base
[0.047s][info   ][class,load] java.net.AbstractPlainSocketImpl source: jrt:/java.base
[0.047s][info   ][class,load] java.net.PlainSocketImpl source: jrt:/java.base
[0.047s][info   ][class,load] java.net.AbstractPlainSocketImpl$1 source: jrt:/java.base
[0.047s][info   ][class,load] sun.net.ext.ExtendedSocketOptions source: jrt:/java.base
[0.047s][info   ][class,load] jdk.net.ExtendedSocketOptions source: jrt:/jdk.net
[0.047s][info   ][class,load] java.net.SocketOption source: jrt:/java.base
[0.047s][info   ][class,load] jdk.net.ExtendedSocketOptions$ExtSocketOption source: jrt:/jdk.net
[0.047s][info   ][class,load] jdk.net.SocketFlow source: jrt:/jdk.net
[0.047s][info   ][class,load] jdk.net.ExtendedSocketOptions$PlatformSocketOptions source: jrt:/jdk.net
[0.047s][info   ][class,load] jdk.net.ExtendedSocketOptions$PlatformSocketOptions$1 source: jrt:/jdk.net
[0.048s][info   ][class,load] jdk.net.LinuxSocketOptions source: jrt:/jdk.net
[0.048s][info   ][class,load] jdk.net.LinuxSocketOptions$$Lambda$2/0x0000000800b51040 source: jdk.net.LinuxSocketOptions
[0.049s][info   ][class,load] jdk.net.ExtendedSocketOptions$1 source: jrt:/jdk.net
[0.049s][info   ][class,load] java.net.StandardSocketOptions source: jrt:/java.base
[0.049s][info   ][class,load] java.net.StandardSocketOptions$StdSocketOption source: jrt:/java.base
[0.051s][info   ][class,load] sun.net.ext.ExtendedSocketOptions$$Lambda$3/0x0000000800b51440 source: sun.net.ext.ExtendedSocketOptions
[0.057s][info   ][class,load] java.net.DelegatingSocketImpl source: jrt:/java.base
[0.057s][info   ][class,load] java.net.SocksSocketImpl source: jrt:/java.base
[0.058s][info   ][class,load] java.net.ServerSocket source: jrt:/java.base
[0.058s][info   ][class,load] jdk.internal.access.JavaNetSocketAccess source: jrt:/java.base
[0.058s][info   ][class,load] java.net.ServerSocket$1 source: jrt:/java.base
复制代码

The above results, the old implementation java.net.PlainSocketImpl been used up.

Switch Expressions (Preview)

Switch 12 is introduced in the JDK expression as a preview feature. JEP 354 modifies this feature, it introduces yield statement for the return value. This means, switch expression (return value) should use the yield, switch statements (does not return a value) should use the break.

In the past, we want to return the contents of the switch, it is quite troublesome, the general syntax is as follows:

int i;
switch (x) {
    case "1":
        i=1;
        break;
    case "2":
        i=2;
        break;
    default:
        i = x.length();
        break;
}
复制代码

Use the following syntax in JDK13 in:

int i = switch (x) {
    case "1" -> 1;
    case "2" -> 2;
    default -> {
        int len = args[1].length();
        yield len;
    }
};
复制代码

or

int i = switch (x) {
    case "1": yield 1;
    case "2": yield 2;
    default: {
        int len = args[1].length();
        yield len;
    }
};
复制代码

After that, switch in on more than one keyword is used out of switch block, and that is yield, he used to return a value. And the difference is that the return: return directly out of the current cycle or the method, the yield will switch out of the current block.

Text Blocks (Preview)

12 introduced Raw String Literals characteristics JDK, but before the release to give up. The JEP the introduction of multi-line strings of text (text block) is similar in meaning.

text block, the text block is a multi-line text string, it avoids the need for most of the escape sequence in a predictable manner automatically formatted string, and allow developers to control the format, as needed.

We used to copy a text string from the outside to Java, will be automatically escaped, if some of the following string:

 <html>
  <body>
      <p>Hello, world</p>
  </body>
</html>
复制代码

Copy it to a Java string, we will show to the following:

"<html>\n" +
"    <body>\n" +
"        <p>Hello, world</p>\n" +
"    </body>\n" +
"</html>\n";
复制代码

Automatically escaped i.e., this seems not very intuitive string, the JDK 13, you can use the following syntax:

"""
<html>
  <body>
      <p>Hello, world</p>
  </body>
</html>
""";
复制代码

Use "", "text block as a start terminator compliance, which can be placed in a string of multiple lines, does not require any escaped. It looks very refreshing.

Such as the common SQL statement:

String query = """
    SELECT `EMP_ID`, `LAST_NAME` FROM `EMPLOYEE_TB`
    WHERE `CITY` = 'INDIANAPOLIS'
    ORDER BY `EMP_ID`, `LAST_NAME`;
""";
复制代码

It looks more intuitive, refreshing.

to sum up

The above is a JDK13 included five characteristics, can change the developer's coding style mainly Text Blocks Switch Expressions and two new features, but these two features still in the preview stage.

Moreover, JDK13 not LTS (long term support) version, if you are using Java 8 (LTS) or Java 11 (LTS), temporarily do not have to upgrade to Java 13.

Reference: openjdk.java.net/projects/jd... metebalci.com/blog/what-i... www.jianshu.com/p/890196bf5...

Guess you like

Origin juejin.im/post/5d7ee7e65188253849631823