Introduction to Java 15

Introduction to Java 15

Java 15 is the next on-time release (September 15,2020), it will bring 15 new features.



Overview

New features:

  • JEP 339: Edwards-curve Digital Signature Algorithm (EdDSA)
  • JEP 371: Hidden classes

Added to the standard

  • JEP 378: Text Blocks
  • JEP 372: Z Garbage Collector (ZGC)
  • JEP 379: Shenandoah

Modernizing

  • JEP 373: Reimplement the legacy DatagramSocket API

Looking forward

  • JEP 360: Sealed classes (Preview)
  • JEP 375: Pattern matching of instanceof (Second preview)
  • JEP 384:Records (Second preview)
  • JEP 383: Foreign-memory access API (Second incubator)

Deprecations & removals

  • JEP 374: Deprecate biased locking
  • JEP 385: Deprecate RMI activation
  • JEP 372: Remove Nashorn
  • JEP 381: Remove Solaris and SPARC ports

https://openjdk.java.net/projects/jdk/15/



JEP 339: Edwards-curve Digital Signature Algorithm (EdDSA)

Platform-independent implementation of EdDSA with better performance than the existing ECDSA implementation (which uses native C code) at the same security strength.

Aims to prevent side-channel attacks:

  • Timing is independent of secrets
  • Will not branch on secrets



JEP 371: Hidden classes

Classes that cannot be used directly by the bytecode of other classes.

Intended for use by frameworks that generate classes at runtime and use them indirectly, via reflection.

Allows us to deprecate the non-standard API sun.misc.Unsafe::defineAnonymousClass, with the intent to deprecate it for removal in a future release.



JEP 378: Text Blocks (previewed in JDK 13 & 14)

A text block is a multi-line string literal that avoids the need for most escape sequences, automatically formats the string in a predictable way, and gives the developer control over the format when desired.

To allow finer control of the processing of newlines and white space, this second preview introduces two new escape sequences.

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

becomes

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

\ escape sequence

\<line-terminator> escape sequence explicitly suppresses the insertion of a newline character.

String literal = "Lorem ipsum dolor sit amet, "+
                 "consectetur adipiscing elit, "+
                 "sed do eiusmod tempor incidiunt";

With \<line-terminator>

String literal = """
                 Lorem ipsum dolor sit amet, \
                 consectetur adipiscing elit, \
                 sed do eiusmod tempor incidiunt\
                 """;

(Both represent the same long line, not multi-line)

\s escape sequence

\s escape sequence can prevent the stripping of trailing white space.

String colors = """
        red  \s
        green
        blue \s\
        """;

(Represents 3 lines of exactly 5 characters long each)



JEP 372: Z Garbage Collector (ZGC)

Change the Z garbage collector from an experimental feature into a product feature.

ZGC, is a concurrent, NUMA-aware, scalable low latency garbage collector, geared to delivering <10 ms GC pauses even on multi-terabytes heaps.

-XX:+UnlockExperimentalVMOptions option no longer needed.

The default GC remains G1.



JEP 379: Shenandoah

Change the Shenandoah Garbage Collector from an experimental feature into a product feature.

-XX:+UnlockExperimentalVMOptions option no longer needed.

The default GC remains G1.



JEP 373: Reimplement the legacy DatagramSocket API

Replace the underlying impplementations of the java.net.DatagramSocket and java.net.MulticastSocket APIs with simpler and more modern implementations that are easy to maintain and debug.

To reduce the risk of switching the implementation after more than twenty years, the legacy implementation will not be removed.

A JDK-specific system property, jdk.net.usePlainDatagramSocketImpl, is introduced to configure the JDK to use the legacy implementation.



JEP 360: Sealed classes (Preview)

Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them.

  • Allow the author of a class or interface to control which code is responsible for implementing it.
  • Provide a more declarative way than access modifiers to restrict the use of a superclass.
  • Support future directions in pattern matching by underpinning the exhaustive analysis of patterns.

Sealed class constraints

  • The sealed class and its permitted subclasses must belong to the same module, and , if declared in an unnamed module, the same package.
  • Every permitted subclass must directly extend the sealed class.
  • Every permitted subclass must choose a modifier to describe how it continues the sealing initiated by its superclass; final sealed, or non-sealed (a sealed class cannot prevent its permitted subclasses fom doing this.)
package com.example.geometry;

public sealed class Shape
        permits com.example.polar.Circle,
                com.example.quad.Rectangle,                
                com.example.quad.simple.Square {...}




JEP 375: Pattern matching of instanceof (Second preview)

Enhance the Java programming language with pattern matching for the instanceof operator.

Pattern matching allows common logic in a program, namely the conditional extraction of components from objects, to be expressed more concisely and safely.

if(obj instanceof String){
   String s=(String)obj;\
   //use s
}

Three things going on here above:

  1. a test: is obj a String
  2. declaration of a new variable s
  3. casting of obj to String into variable s

With JEP 375, we get to:

if(obj instanceof String s){
   //use s
} else {
   //s is out of scope here!
}

Pattern matches handles conditionals as you would expect...

if(obj instanceof String s && s.length()>5){ .. s.contains(..)..}
                           //"s" is in scope here

When using conditional or...

if(obj instanceof String s || s.length()>5){ .. s..}
                           //"s" is not defined  here

Error(8:40) java: cannot find symbol
symbol: variable s
location: class Main



JEP 384:Records (Second preview)

Records provide a compact syntax for declaring classes which are transparent holders for shallowly immutable data.

class Point {
   final int x;
   final int y;
   
   public Point(int x, inty) {
      this.x = x;
      this.y = y;
   }

   @Override
   public boolean equals(Object o) {
       if (this == o) return true;
       if (o == null || getClass() != o.getClass()) return false;

       Point point = (Point) o;

       if (x != point.x) return false;
       return y == point.y;
   }

   @Override
   public int hashCode() {
      int result = x;
      result = 31 * result + y;
      return result;
   }

   @Override
   public String toString() {
      return "Point{x=" + x + ", y=" + y + '}';
   }

   public int x() { return x; }

   public int y() { return y; }

becomes

record Point {int x; int y;}

Any of the members that are automatically derived from the state description can also be declared explicitly.

Local Records - Added in Second preview

List<Merchant> findTopMerchants(List<Merchant> merchants, int month) {
    //Local record
    record MerchantSales(Merchant merchant, double sales) {}

    return merchants.stream()
     .map(merchant -> new MerchantSales(merchant, computeSales(merchant, month)))
     .sorted((m1,m2) -> Double.compare(m2.sales(), m1.sales()))
     .map(MerchantSales::merchant)
     .collect(toList));
}



JEP 383: Foreign-memory access API (Second incubator)

API to allow Java programs to safely and efficiently access foreign memory outside of the Java heap.

Aims to provide a valid alternative to the main avenues by which Java programs access foreign memory today, namely java.nio.ByteBuffer and sun.misc.Unsafe.

First major content from Project Panama, a project with the goal of improving connections between Java and non-Java APIs.



Deprecations

  • Disable and deprecate Biased locking (JEP 374)
  • RMI activation for removal (JEP 385)



Removals

  • Nashorn Java Script Engine (JEP 372) --Deprecated for removal since JDK 11--
  • Solaris and SPARC ports (JEP 381) --Deprecated for removal sinc JDK14--

Source code for these features remains available

要查看或添加评论,请登录

Vincent Vauban的更多文章

  • [VV113] The Java 21 Newsletter

    [VV113] The Java 21 Newsletter

    ????2??1?? Dear followers, let's prepare for Java 21 certification together! 1?? How would you answer this question:…

  • ?? Broken Function Level Authorization – API5:2023 ??

    ?? Broken Function Level Authorization – API5:2023 ??

    I'm kicking off a series of articles on API Security ?? to help us—developers ????????—better understand and implement…

  • [VV112] The Java 21 Newsletter

    [VV112] The Java 21 Newsletter

    ????2??1?? Dear followers, let's prepare for Java 21 certification together! 1?? How would you answer this question:…

  • FR Les mots sans les maux. ???

    FR Les mots sans les maux. ???

    FR Hier, j’ai eu la chance d’assister à un atelier sur la Communication Non Violente (CNV) avec les superbes people de…

  • ?? Unrestricted Resource Consumption – API4:2023 ??

    ?? Unrestricted Resource Consumption – API4:2023 ??

    I'm kicking off a series of articles on API Security ?? to help us—developers ????????—better understand and implement…

  • ?? Broken Object Property Level Authorization – API3:2023 ??

    ?? Broken Object Property Level Authorization – API3:2023 ??

    I'm kicking off a series of articles on API Security ?? to help us—developers ????????—better understand and implement…

  • [VV111] The Java 21 Newsletter

    [VV111] The Java 21 Newsletter

    ????2??1?? Dear followers, let's prepare for Java 21 certification together! 1?? How would you answer this question:…

    18 条评论
  • ?? Broken Authentication – API2:2023 ??

    ?? Broken Authentication – API2:2023 ??

    I'm kicking off a series of articles on API Security ?? to help us—developers ????????—better understand and implement…

  • ?? BOLA – The #1 API Security Threat (API1:2023)

    ?? BOLA – The #1 API Security Threat (API1:2023)

    I'm kicking off a series of articles on API Security ?? to help us—developers ??????????—better understand and…

  • [VV110] The Java 21 Newsletter

    [VV110] The Java 21 Newsletter

    ????2??1?? Dear followers, let's prepare for Java 21 certification together! 1?? How would you answer this question:…

社区洞察

其他会员也浏览了