温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java15有什么新特性

发布时间:2021-11-24 16:02:46 来源:亿速云 阅读:171 作者:iii 栏目:大数据

本篇内容主要讲解“Java15有什么新特性”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java15有什么新特性”吧!

版本号

java -version
openjdk version "15" 2020-09-15
OpenJDK Runtime Environment (build 15+36-1562)
OpenJDK 64-Bit Server VM (build 15+36-1562, mixed mode, sharing)

从version信息可以看出是build 15+36

特性列表

339:Edwards-Curve Digital Signature Algorithm (EdDSA)

新增rfc8032描述的Edwards-Curve Digital Signature Algorithm (EdDSA)实现 使用示例

// example: generate a key pair and sign
KeyPairGenerator kpg = KeyPairGenerator.getInstance("Ed25519");
KeyPair kp = kpg.generateKeyPair();
// algorithm is pure Ed25519
Signature sig = Signature.getInstance("Ed25519");
sig.initSign(kp.getPrivate());
sig.update(msg);
byte[] s = sig.sign();

// example: use KeyFactory to contruct a public key
KeyFactory kf = KeyFactory.getInstance("EdDSA");
boolean xOdd = ...
BigInteger y = ...
NamedParameterSpec paramSpec = new NamedParameterSpec("Ed25519");
EdECPublicKeySpec pubSpec = new EdECPublicKeySpec(paramSpec, new EdPoint(xOdd, y));
PublicKey pubKey = kf.generatePublic(pubSpec);

360:Sealed Classes (Preview)

JDK15引入了sealed classes and interfaces.用于限定实现类,限定父类的使用,为后续的pattern matching的exhaustive analysis提供便利 示例

package com.example.geometry;

public abstract sealed class Shape
    permits Circle, Rectangle, Square {...}

public final class Circle extends Shape {...}

public sealed class Rectangle extends Shape 
    permits TransparentRectangle, FilledRectangle {...}
public final class TransparentRectangle extends Rectangle {...}
public final class FilledRectangle extends Rectangle {...}

public non-sealed class Square extends Shape {...}

这里使用了3个关键字,一个是sealed,一个是permits,一个是non-sealed;permits的这些子类要么使用final,要么使用sealed,要么使用non-sealed修饰;针对record类型,也可以使用sealed,因为record类型暗示这final

package com.example.expression;

public sealed interface Expr
    permits ConstantExpr, PlusExpr, TimesExpr, NegExpr {...}

public record ConstantExpr(int i)       implements Expr {...}
public record PlusExpr(Expr a, Expr b)  implements Expr {...}
public record TimesExpr(Expr a, Expr b) implements Expr {...}
public record NegExpr(Expr e)           implements Expr {...}

371:Hidden Classes

JDK15引入了Hidden Classes,同时废弃了非标准的sun.misc.Unsafe::defineAnonymousClass,目标是为frameworks提供在运行时生成内部的class

372:Remove the Nashorn JavaScript Engine

JDK15移除了Nashorn JavaScript Engine及jjs tool,它们在JDK11的被标记为废弃;具体就是jdk.scripting.nashorn及jdk.scripting.nashorn.shell这两个模块被移除了

373:Reimplement the Legacy DatagramSocket API

该特性使用更简单及更现代的方式重新实现了java.net.DatagramSocket及java.net.MulticastSocket以方便更好的维护及debug,新的实现将会更容易支持virtual threads

374:Disable and Deprecate Biased Locking

该特性默认禁用了biased locking(-XX:+UseBiasedLocking),并且废弃了所有相关的命令行选型(BiasedLockingStartupDelay, BiasedLockingBulkRebiasThreshold, BiasedLockingBulkRevokeThreshold, BiasedLockingDecayTime, UseOptoBiasInlining, PrintBiasedLockingStatistics and PrintPreciseBiasedLockingStatistics)

375:Pattern Matching for instanceof (Second Preview)

instanceof的Pattern Matching在JDK15进行Second Preview,示例如下:

public boolean equals(Object o) { 
    return (o instanceof CaseInsensitiveString cis) && 
        cis.s.equalsIgnoreCase(s); 
}

377:ZGC: A Scalable Low-Latency Garbage Collector

ZGC在JDK11被作为experimental feature引入,在JDK15变成Production,但是这并不是替换默认的GC,默认的GC仍然还是G1;之前需要通过-XX:+UnlockExperimentalVMOptions -XX:+UseZGC来启用ZGC,现在只需要-XX:+UseZGC就可以 相关的参数有ZAllocationSpikeTolerance、ZCollectionInterval、ZFragmentationLimit、ZMarkStackSpaceLimit、ZProactive、ZUncommit、ZUncommitDelay ZGC-specific JFR events(ZAllocationStall、ZPageAllocation、ZPageCacheFlush、ZRelocationSet、ZRelocationSetGroup、ZUncommit)也从experimental变为product

378:Text Blocks

Text Blocks在JDK13被作为preview feature引入,在JDK14作为Second Preview,在JDK15变为最终版

379:Shenandoah: A Low-Pause-Time Garbage Collector

Shenandoah在JDK12被作为experimental引入,在JDK15变为Production;之前需要通过-XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC来启用,现在只需要-XX:+UseShenandoahGC即可启用

381:Remove the Solaris and SPARC Ports

Solaris and SPARC Ports在JDK14被标记为废弃,在JDK15版本正式移除

383:Foreign-Memory Access API (Second Incubator)

Foreign-Memory Access API在JDK14被作为incubating API引入,在JDK15处于Second Incubator

384:Records (Second Preview)

Records在JDK14被作为preview引入,在JDK15处于Second Preview

385:Deprecate RMI Activation for Removal

JDK15废弃了RMI Activation,后续将被移除

细项解读

上面列出的是大方面的特性,除此之外还有一些api的更新及废弃,主要见JDK 15 Release Notes,这里举几个例子。

添加项

  • Support for Unicode 13.0 (JDK-8239383)

升级了Unicode,支持Unicode 13.0

  • Added isEmpty Default Method to CharSequence (JDK-8215401)

给CharSequence新增了isEmpty方法 java.base/java/lang/CharSequence.java

    /**
     * Returns {@code true} if this character sequence is empty.
     *
     * @implSpec
     * The default implementation returns the result of calling {@code length() == 0}.
     *
     * @return {@code true} if {@link #length()} is {@code 0}, otherwise
     * {@code false}
     *
     * @since 15
     */
    default boolean isEmpty() {
        return this.length() == 0;
    }
  • Added Support for SO_INCOMING_NAPI_ID Support (JDK-8243099)

jdk.net.ExtendedSocketOptions新增了SO_INCOMING_NAPI_ID选型

  • Specialized Implementations of TreeMap Methods (JDK-8176894)

JDK15对TreeMap提供了putIfAbsent, computeIfAbsent, computeIfPresent, compute, merge方法提供了overriding实现

  • New Option Added to jcmd for Writing a gzipped Heap Dump (JDK-8237354)

jcmd的GC.heap_dump命令现在支持gz选型,以dump出gzip压缩版的heap;compression level从1(fastest)到9(slowest, but best compression),默认为1

  • New System Properties to Configure the TLS Signature Schemes (JDK-8242141)

新增了jdk.tls.client.SignatureSchemesjdk.tls.server.SignatureSchemes用于配置TLS Signature Schemes

  • Support for certificate_authorities Extension (JDK-8206925)

支持certificate_authorities的扩展

移除项

  • Obsolete -XX:UseAdaptiveGCBoundary (JDK-8228991)

淘汰了-XX:UseAdaptiveGCBoundary

废弃项

  • Deprecated -XX:ForceNUMA Option (JDK-8243628)

废弃了ForceNUMA选项

  • Disable Native SunEC Implementation by Default (JDK-8237219)

默认禁用了Native SunEC Implementation

已知问题

  • java.net.HttpClient Does Not Override Protocols Specified in SSLContext Default Parameters (JDK-8239594)

HttpClient现在没有覆盖在SSLContext Default Parameters中指定的Protocols

其他事项

  • DatagramPacket.getPort() Returns 0 When the Port Is Not Set (JDK-8237890)

当DatagramPacket没有设置port的时候,其getPort方法返回0

  • Improved Ergonomics for G1 Heap Region Size (JDK-8241670)

优化了默认G1 Heap Region Size的计算

到此,相信大家对“Java15有什么新特性”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI