What is new in Java 13

Java logo

Java 13 reached General Availability on 17 September 2019, download Java 13 here.

Some new features in Java 13

1. JEP 350 Dynamic CDS Archives

This JEP enhanced the JEP 310 Application Class-Data Sharing which was introduced in Java 10, by simplifying the process of creating CDS archives.

The CDS archives will be created if the program exists with -XX:ArchiveClassesAtExit


$ java -XX:ArchiveClassesAtExit=hello.jsa -cp hello.jar Hello

To run the program with the CDS archives above.


$  bin/java -XX:SharedArchiveFile=hello.jsa -cp hello.jar Hello

The idea behind Class Data Sharing (CDS) is a feature to improve startup performance by creating class-data archive once and then reuse it, so that the JVM need not recreate it again.

Please read articles below to understand more about CDS:

2. JEP 351 ZGC: Uncommit Unused Memory

The JEP 333: Z Garbage Collector is introduced in Java 11, it provides a short pause times when cleaning up heap memories. However, it didn’t return unused heap memory to the operating system, even when that memory has been unused for a long time.

This JEP enhanced the ZGC by returning unused heap memory to the operating system.

3. JEP-353 Reimplement the Legacy Socket API

The underlying implementations of java.net.Socket and java.net.ServerSocket is very old, date back to JDK 1.0, it is a mix of legacy Java and C code that is hard to maintain and debug. This JEP introduces a new underlying implementations for the Socket APIs and it is the default implementation in Java 13.

Before Java 13, it uses the PlainSocketImpl for the SocketImpl

ServerSocket.java

public class ServerSocket implements java.io.Closeable {
    
    /**
     * The implementation of this Socket.
     */
    private SocketImpl impl;
	
}

In Java 13, it introduces a new NioSocketImpl class as a drop-in replacement for PlainSocketImpl. However, if something goes wrong, we can still switch back to the old implementation PlainSocketImpl by setting jdk.net.usePlainSocketImpl system property

Review a simple Socket example.

JEP353.java

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class JEP353 {

    public static void main(String[] args) {

        try (ServerSocket serverSocket = new ServerSocket(8888)){

            boolean running = true;
            while(running){

                Socket clientSocket = serverSocket.accept();
                //do something with clientSocket
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Trace a class loading for the above Socket class. In Java 13, the default implementation is NioSocketImpl

Terminal

D:\test>javac JEP353.java

D:\test>java JEP353

D:\test>java -XX:+TraceClassLoading JEP353  | findStr Socket

[0.040s][info   ][class,load] java.net.ServerSocket source: jrt:/java.base
[0.040s][info   ][class,load] jdk.internal.access.JavaNetSocketAccess source: jrt:/java.base
[0.040s][info   ][class,load] java.net.ServerSocket$1 source: jrt:/java.base
[0.040s][info   ][class,load] java.net.SocketOptions source: jrt:/java.base
[0.040s][info   ][class,load] java.net.SocketImpl source: jrt:/java.base
[0.044s][info   ][class,load] java.net.SocketImpl$$Lambda$1/0x0000000800ba0840 source: java.net.SocketImpl
[0.047s][info   ][class,load] sun.net.PlatformSocketImpl source: jrt:/java.base

[0.047s][info   ][class,load] sun.nio.ch.NioSocketImpl source: jrt:/java.base

[0.047s][info   ][class,load] sun.nio.ch.SocketDispatcher source: jrt:/java.base
[0.052s][info   ][class,load] java.net.SocketAddress source: jrt:/java.base
[0.052s][info   ][class,load] java.net.InetSocketAddress source: jrt:/java.base
[0.052s][info   ][class,load] java.net.InetSocketAddress$InetSocketAddressHolder source: jrt:/java.base
[0.053s][info   ][class,load] sun.net.ext.ExtendedSocketOptions source: jrt:/java.base
[0.053s][info   ][class,load] jdk.net.ExtendedSocketOptions source: jrt:/jdk.net
[0.053s][info   ][class,load] java.net.SocketOption source: jrt:/java.base
[0.053s][info   ][class,load] jdk.net.ExtendedSocketOptions$ExtSocketOption source: jrt:/jdk.net
[0.053s][info   ][class,load] jdk.net.SocketFlow source: jrt:/jdk.net
[0.053s][info   ][class,load] jdk.net.ExtendedSocketOptions$PlatformSocketOptions source: jrt:/jdk.net
[0.053s][info   ][class,load] jdk.net.ExtendedSocketOptions$PlatformSocketOptions$1 source: jrt:/jdk.net
[0.054s][info   ][class,load] jdk.net.ExtendedSocketOptions$1 source: jrt:/jdk.net
[0.054s][info   ][class,load] sun.nio.ch.NioSocketImpl$FileDescriptorCloser source: jrt:/java.base
[0.055s][info   ][class,load] java.net.Socket source: jrt:/java.base

We can switch back to PlainSocketImpl by setting the Djdk.net.usePlainSocketImpl system property.

Terminal

D:\test>java -Djdk.net.usePlainSocketImpl -XX:+TraceClassLoading JEP353  | findStr Socket

[0.041s][info   ][class,load] java.net.ServerSocket source: jrt:/java.base
[0.041s][info   ][class,load] jdk.internal.access.JavaNetSocketAccess source: jrt:/java.base
[0.041s][info   ][class,load] java.net.ServerSocket$1 source: jrt:/java.base
[0.041s][info   ][class,load] java.net.SocketOptions source: jrt:/java.base
[0.041s][info   ][class,load] java.net.SocketImpl source: jrt:/java.base
[0.045s][info   ][class,load] java.net.SocketImpl$$Lambda$1/0x0000000800ba0840 source: java.net.SocketImpl
[0.048s][info   ][class,load] sun.net.PlatformSocketImpl source: jrt:/java.base
[0.048s][info   ][class,load] java.net.AbstractPlainSocketImpl source: jrt:/java.base

[0.048s][info   ][class,load] java.net.PlainSocketImpl source: jrt:/java.base

[0.048s][info   ][class,load] java.net.AbstractPlainSocketImpl$1 source: jrt:/java.base
[0.050s][info   ][class,load] sun.net.ext.ExtendedSocketOptions source: jrt:/java.base
[0.050s][info   ][class,load] jdk.net.ExtendedSocketOptions source: jrt:/jdk.net
[0.050s][info   ][class,load] java.net.SocketOption source: jrt:/java.base
[0.051s][info   ][class,load] jdk.net.ExtendedSocketOptions$ExtSocketOption source: jrt:/jdk.net
[0.051s][info   ][class,load] jdk.net.SocketFlow source: jrt:/jdk.net
[0.051s][info   ][class,load] jdk.net.ExtendedSocketOptions$PlatformSocketOptions source: jrt:/jdk.net
[0.051s][info   ][class,load] jdk.net.ExtendedSocketOptions$PlatformSocketOptions$1 source: jrt:/jdk.net
[0.051s][info   ][class,load] jdk.net.ExtendedSocketOptions$1 source: jrt:/jdk.net
[0.051s][info   ][class,load] java.net.StandardSocketOptions source: jrt:/java.base
[0.051s][info   ][class,load] java.net.StandardSocketOptions$StdSocketOption source: jrt:/java.base
[0.053s][info   ][class,load] sun.net.ext.ExtendedSocketOptions$$Lambda$2/0x0000000800ba1040 source: sun.net.ext.ExtendedSocketOptions
[0.056s][info   ][class,load] java.net.SocketAddress source: jrt:/java.base
[0.056s][info   ][class,load] java.net.InetSocketAddress source: jrt:/java.base
[0.058s][info   ][class,load] java.net.InetSocketAddress$InetSocketAddressHolder source: jrt:/java.base
[0.059s][info   ][class,load] java.net.SocketCleanable source: jrt:/java.base

4. JEP-354 Switch Expressions (Preview)

This JEP enhanced the previous Java 12 JEP 325 Switch expressions so it can be used as either a statement (NOT returning something) or an expression (returning something). A new keyword ‘yield’ is introduced to return a value from the switch.

P.S This is a preview language feature in Java 13

Before Java 12, traditional switch statement, we can return a value like this:


	private static String getText(int number) {
        String result = "";
        switch (number) {
            case 1, 2:
                result = "one or two";
                break;
            case 3:
                result = "three";
                break;
            case 4, 5, 6:
                result = "four or five or six";
                break;
            default:
                result = "unknown";
                break;
        };
        return result;
    }

In Java 12, we can use break to return a value from a switch.


	private static String getText(int number) {
        String result = switch (number) {
            case 1, 2:
                break "one or two";
            case 3:
                break "three";
            case 4, 5, 6:
                break "four or five or six";
            default:
                break "unknown";
        };
        return result;
    }

In Java 13, the above Java 12 value break is no longer compiled, we should use yield to return a value.


	private static String getText(int number) {
        return switch (number) {
            case 1, 2:
                yield "one or two";
            case 3:
                yield "three";
            case 4, 5, 6:
                yield "four or five or six";
            default:
                yield "unknown";
        };
    }

The Java 12 switch rule labels or arrow syntax is still supported in Java 13.


	private static String getText(int number) {
        return switch (number) {
            case 1, 2 -> "one or two";
            case 3 -> "three";
            case 4, 5, 6 -> "four or five or six";
            default -> "unknown";
        };
    }
Note
For full example, please read this Java 13 Switch Expressions

5. JEP-355 Text Blocks (Preview)

This JEP introduces multi-line string literal, a text block, finally.

P.S This is a preview language feature in Java 13

Before Java 13


 String html ="<html>\n" +
			  "   <body>\n" +
			  "      <p>Hello, World</p>\n" +
			  "   </body>\n" +
			  "</html>\n";

			  
 String json ="{\n" +
			  "   \"name\":\"mkyong\",\n" +
			  "   \"age\":38\n" +
			  "}\n";

Now Java 13


 String html = """
                <html>
                    <body>
                        <p>Hello, World</p>
                    </body>
                </html>
				""";

 String json = """
                {
                    "name":"mkyong",
                    "age":38
                }
                """;
To enable the Java 13 preview features:


javac --enable-preview --release 13 Example.java
java --enable-preview Example

References

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
5 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
5 Comment authors
Prashant TiwariguilhermeFrancooMadanFede Recent comment authors
newest oldest most voted
Madan
Guest
Madan

Thanks bro it’s really helpful

Fede
Guest
Fede

Great! Thanks !

guilherme
Guest
guilherme

thanks for compiling it, great article

Francoo
Guest
Francoo

Nice

Prashant Tiwari
Guest
Prashant Tiwari

Great! Thanks !