Java 16 released

The 16th version of the Java SE platform has been released. This release contains about two and a half thousand closed problems and 17 JEPs. API changes can be viewed here . Release notes here .







The Oracle JDK and OpenJDK distributions are already available for download .









The JEPs that made it into Java 16 will be broken down into four categories: language, API, JVM, and infrastructure.







Tongue



Pattern matching for the instanceof operator (JEP 375)



The instanceof



pattern-matching operator, which was introduced in Java 14 and moved to the second preview in Java 15, is now a stable syntax and no longer requires a flag --enable-preview



. We discussed pattern matching in detail in this article , and since then two changes have been made to it:







First, pattern variables are no longer implicitly final:







if (obj instanceof String s) {
    s = "Hello"; // OK  Java 16,   Java 15
}
      
      





Second, if the type of the expression known at compile time is a subtype of the type being checked, then it is now a compilation error:







String str = ...
if (str instanceof String s) { // O  Java 16, OK  Java 15
}
      
      







Entries (JEP 395)



, โ€“ . preview Java 14 Java 15. . Java 16 : :







public class Outer {
    public class Inner {
        // OK  Java 16,   Java 15
        static void main(String[] args) {
        }

        // OK  Java 16,   Java 15
        record Point(int x, int y) {
        }
    }
}
      
      







sealed



( preview) (JEP 397)



ยซยป , Java 15 preview, . . :







  • Java contextual keyword restricted keyword restricted identifier, contextual keywords sealed



    , non-sealed



    permits



    .
  • , sealed



    :
    sealed interface Sealed {
    }
    
    final class Impl implements Sealed {
        void f(Runnable r) {
            Sealed s = (Sealed) r; // error: incompatible types
        }
    }
          
          



  • Class.permittedSubclasses()



    Class.getPermittedSubclasses()



    .




JVM



JDK (JEP 396)



API JDK, Java 9, : Java 9-15 --illegal-access



permit



, Java 16 deny



. , API (sun.*



, com.sun.*



, jdk.internal.*



..) .







JDK , Java 16, JVM:







  • --illegal-access=permit/warn/debug



    : JDK
  • --add-opens=module/package=target-module



    :
  • --add-exports=module/package=target-module



    : ( )


--illegal-access



. Java 16, : Option --illegal-access is deprecated and will be removed in a future release



.







API jdk.unsupported



: sun.misc



sun.reflect



.









Warnings for Value-Based Classes (JEP 390)



- (Integer



, Double



, Character



..) value-based , , deprecated Java 9, deprecated for removal.







value-based API Java 8. , , , identity: ==



, , identityHashCode()



.. Value-based Valhalla, .







value-based :







Double d = 0.0;
synchronized (d) { // warning: [synchronization] attempt to synchronize on an instance of a value-based class
}
      
      





value-based JVM:







  • -XX:+UnlockDiagnosticVMOptions -XX:DiagnoseSyncOnValueBasedClasses=1



    : .
  • -XX:+UnlockDiagnosticVMOptions -XX:DiagnoseSyncOnValueBasedClasses=2



    : .




ZGC: Concurrent Thread-Stack Processing (JEP 376)



ZGC safepoints . .









Unix-Domain Socket Channels (JEP 380)



Unix socket channel server-socket channel API. , , . Unix Windows 10 Windows Server 2019.









Elastic Metaspace (JEP 387)



Metaspace ( JVM, ) . , .









Alpine Linux Port (JEP 386)



JDK Alpine Linux Linux, musl C. Alpine Linux , . JDK .









Windows/AArch64 Port (JEP 388)



JDK Windows/AArch64. Java Windows on ARM, .









API



Stream





java.util.stream.Stream



JEP, , .







โ€“ Stream.toList()



. Stream



. , Collectors.toUnmodifiableList()



, , Stream.toList()



, null



-.







โ€“ Stream.mapMulti()



( ). Stream.flatMap()



: flatMap()



, Stream



, mapMulti()



, โ€“ , โ€“ Consumer, . :







IntStream.rangeClosed(1, 10).mapMulti((i, consumer) -> {
    for (int j = 1; j <= i; j++) {
        consumer.accept(j);
    }
}); //  1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, ...
      
      







(JEP 392)



jpackage



, Java 14 , .









Vector API (Incubator) (JEP 338)



SIMD- (x64 AArch64). API , JVM . , , .







API jdk.incubator.vector



.









Foreign Linker API (Incubator) (JEP 389)



API, Panama โ€“ Foreign Linker API. Java, JNI: , .







Foreign API Oracle.









Foreign-Memory Access API (Third Incubator) (JEP 393)



API Java, Java 14, .











Enable C++14 Language Features (JEP 347)



JDK Java 16 C++98/03. Java 11 , C++11/14. : HotSpot C++11/14, .









Migrate from Mercurial to Git (JEP 357) Migrate to GitHub (JEP 369)



JDK Git GitHub. 2020 , Java 16 .







GitHub . -, . Skara.







Git JDK: jdk11u , , jdk8u.









Java 16 STS-, .









If you do not want to miss the news about Java, then subscribe to the miniJUG Telegram channel








All Articles