Gatsby Default Starter

The New Features In Java 13

Written by Puja Majumder on October 23, 2019

This year has witnessed two versions of Java. Java 13 is released on September 17th, 2019 with lots of new features. Among them, ten remarkable features are discussed below:

  1. JEP 350 Dynamic CDS Archives
  2. It started the journey in Java 10 and streamlined the process of the CDS (Class Data Sharing) archive creation. However, the creation of the CDS archives depends on the existence of the program with -XX:ArchiveClassesAtExit.

    The good news is that JEP (JDK Enhancement Proposal) has come with the improved version of the JEP 310 Application Class-Data Sharing.

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

    The code below is for running the program with the CDS archives specified above.

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

    The main goal of CDS is to enhance the performance of start-ups through the creation of a class-data archive for only once and it’s followed by reusing it. Thus, the need of recreation by JVM (Java virtual machine) can be eliminated.

  3. Reimplementation Of Legacy Socket API Is Done By JEP-353
  4. The concepts of java.net.ServerSocket and java.net.Socket implementations belong to JDK 1.0. That means it’s almost outdated nowadays. Since it’s a combination of Java and C code, it’s really difficult to debug as well as maintain.

    However, the JEP has come with new basic implementations dedicated to the Socket APIs. It is actually the default implementation of this version.

    In previous versions, it used the PlainSocketImpl for the SocketImpl.

    ServerSocket.java

    public class ServerSocket implements java.io.Closeable {
    
        /**
    
         * The implementation of this Socket.
    
         */
    
        private SocketImpl impl;
    
    }
  5. JEP-354 Switch Expressions (Preview)
  6. The JEP- in this version- improved the last Java 12 JEP 325 Switch expressions. Thus, it can now be used as either an expression (for returning something) or a statement (for NOT returning something). Furthermore, a new keyword ‘yield’ also started its journey for returning a value from the switch statement.

    When it comes to (PlainSocketImpl)’s drop-in replacement, Java 13 has launched a new NioSocketImpl class.

    Let’s take a look at a Socket example below:

    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();
    
            }
    
        }
    
    }
  7. JEP-355 Text Blocks (Preview)
  8. This JDK Enhancement Proposal has also launched multi-line string literal, which is a text block. It is actually a preview language feature in this Java version.

    Prior to Java 12, programmers can return a value with traditional switch statement as follows:

    private static String getText(int number) {
        String result = "";
        switch (number) {
            case 1:
                result = "one";
                break;
            case 2:
                result = "two";
                break;
            case 3:
                result = "three";
                break;
            default:
                result = "unknown";
                break;
        };
        return result;
    }

    After the introduction of Java 12, programmers used to take help of break in order to return a value from a switch.

    private static String getText(int number) {
        String result =
            switch (number) {
                case 1:
                    break "one";
                case 2:
                    break "two";
                case 3:
                    break "three";
                default:
                    break "unknown";
            };
        return result;
    }

    Now, in Java 13, yield is introduced for returning a value.

    private static String getText(int number) {
        return switch (number) {
            case 1:
                yield "one";
            case 2:
                yield "two";
            case 3:
                yield "three";
            default:
                yield "unknown";
        };
    }
  9. Java 13 Supports For Unicode 12.1 
  10. The new version has upgraded Unicode support to 12.1 that includes the changes below:

    java.lang.Character is compatible with Unicode Character Database of the level of 12.1.

    java.util.regex package aids Extended Grapheme Clusters that depends on 12.0 level of Unicode Standard Annex #29.

    java.text.Normalizer and java.text.Bidi classes support Unicode of 12.0 level Unicode Standard Annexes, number 9 and number 15, respectively.

  11. New String Constants Are Introduced For Canonical XML 1.1 URIs 
  12. INCLUSIVE_11 and INCLUSIVE_11_WITH_COMMENTS are two New String constants and they are added to the javax.xml.crypto.dsig.CanonicalizationMethod API.

  13. This Version Permits The Restriction Of SASL Mechanisms
  14. Java 13 has added a security property, called jdk.sasl.disabledMechanisms and it can deactivate SASL mechanisms. Moreover, any deactivated mechanism would be ignored in case it’s described in the mechanisms argument of Sasl.createSaslServer or the mechanisms argument of Sasl.createSaslClient.

  15. A Default Native GSS-API Library on Windows Has Been Added
  16. This version of Java has added a native GSS-API library to JDK on Windows. Nonetheless, the library is only client-side and it makes use of the default credentials. Once the property, sun.security.jgss.native system gets set to “true”, the GSS-API library will be loaded.

  17. Kerberos Cross-Realm Referrals (RFC 6806) Is Supported
  18. Cross-realm referrals and principal name canonicalization support the Kerberos client for its improvement. The RFC 6806 protocol extension has defined this rule.

  19. Runtime Trace Methods Have Been Removed
  20. The methods, traceMethodCalls(boolean) and traceInstructions(boolean) have become outdated. So, in Java 13, these two methods are eliminated from the java.lang.Runtime class.

    It’s not a comprehensive list. A lot of other features have also been introduced in Java 13.