Java 9至Java 13-主要功能

编程工具,框架变得越来越对开发人员友好,并提供了更好的现代功能来提高开发人员的生产力。 长期以来,Java因拥有缓慢的发布而臭名昭著。 但是,为了与时俱进,Java已经采取了每6个月(每年3月和9月)发布版本升级的方式来发布新功能。 从那时起,每个Java开发人员的工具集中都添加了许多很酷的功能和工具。

这是Java 9到Java 13之间引入的最新功能的快速摘要。

Java 9

模块系统:帮助大型应用程序模块化。 这有助于限制模块中公开的类相对于模块的真正公开api的公开程度。 明确定义依赖项并导出module-info.java。 例如:

module chef {
  exports com.tbst.recipe;

  requires kitchen;
}

在这里,模块厨师取决于模块厨房出口模块食谱。

捷联:具有显式依赖关系和模块化JDK意味着我们可以轻松提出应用程序的整个依赖关系图。 反过来,这使得拥有最小的运行时环境成为可能,该环境仅包含运行应用程序所需的环境。 这可以帮助减小可执行jar的整体大小。

壳:交互式REPL(读取-评估-打印循环),用于快速播放Java代码。 说啊壳在安装JDK 9+之后在终端上运行。

收集工厂方法:以前,必须先初始化集合的实现(地图,集合,列表),然后再向其中添加对象。 最后,可以创建一成不变的具有静态工厂方法签名的集合<Collection>.of()。 这是通过在每个相应接口中使用一堆静态方法来实现的。 例如:

// Creates an immutable list
List<String> abcs = List.of("A", "B", "C");

// Creates an immutable set
Set<String> xyzs = Set.of("X", "Y", "Z");

// Creates an immutable Map
Map<String, String> mappings = Map.of("key1", "value1", "key2", "value2");

其它功能 -Stream API获得更多功能,例如dropWhile,采取,空值。 - Private interface methods to write clean code and keep things DRY when using default methods in interfaces。 - new HTTP2 API that supports streams and server based pushes。


Java 10

局部变量类型推断:这使我们能够编写更现代的Kotlin / Scala / Typescript之类的语法,而不必显式声明变量类型而不损害类型安全性。 在这里,由于赋值时右侧的值的类型,编译器能够找出类型。 例如:

var list = new ArrayList<String>();  // infers ArrayList<String>
var stream = list.stream();          // infers Stream<String>

In cases where the compiler cannot infer the value or it's ambiguous, you need to explicitly declare it. More details here

适用于G1的并行全GC:通过使整个GC并行来提高G1最坏情况的延迟。

基于Java的实验性JIT编译器:使基于Java的JIT编译器Graal可用作Linux / x64平台上的实验性JIT编译器。

备用存储设备上的堆分配:使HotSpot VM可以在用户指定的备用存储设备(例如NV-DIMM)上分配Java对象堆。

JDK中的根证书:将Oracle Java SE Root CA程序中的根证书开源,以使OpenJDK构建对开发人员更具吸引力,并减少这些构建与Oracle JDK构建之间的差异。


Java 11

新的String方法:字符串类获得新的方法,例如isBlank(),lines(),重复(int),unicode aware 跳闸(),stripLeading()和stripTrailing()。

新文件方法:writeString(),readString()和isSameFile()。

Lambda参数的局部变量语法:在声明隐式类型的lambda表达式的形式参数时,允许使用变种。 引入它是为了与变种 for local 变种iables. Eg:

(var x, var y) -> x.process(y)   // implicit typed lambda expression

// One benefit of uniformity is that modifiers, notably annotations, can be applied
// to local variables and lambda variables without losing brevity.
(@Nonnull var x, @Nullable var y) -> x.process(y)

JEP 328:飞行记录器:JFR是一种分析工具,用于从正在运行的Java应用程序中收集诊断信息和分析数据。 它的性能开销可以忽略不计,通常低于1%。 因此,它可以用于生产应用。

删除了Java EE和CORBA模块:删除了以下软件包:java.xml.ws,java.xml.bind,java.activation,java.xml.ws.annotation,java.corba,java.transaction,java.se.ee,jdk.xml.ws,jdk.xml.bind

隐式编译并运行无需使用Java语言第一。 您可以直接使用爪哇命令并隐式编译。 这样做是为了运行作为Java源代码的单个文件提供的程序,包括通过“ shebang”文件和相关技术从脚本中使用该程序。 当然,对于任何大于文件的项目,都可以使用gradle,maven等构建工具。


Java 12 - Released March 19, 2019

切换表情😎新的开关表达式期望返回值。 多个匹配项可以在同一行上用逗号分隔,并且匹配项上的内容标记为->。 不像传统开关, matches don't fall through to the next match。 So you don't have to use 打破; and this helps prevent bugs。 Eg:

String status = process(..., ..., ...);
var isCompleted = switch (status) {
    case "PROCESSED", "COMPLETED" -> true;
    case "WAITING", "STUCK" -> false;
    default -> throw new InconsistentProcessingStateException();
};

switch表达式作为预览引入,需要-启用预览标记到Javac或在IDE中启用它。

File byte comparison with File.mismatch(). (From Javadoc) Finds and returns the position of the first mismatched byte in the content of two files, or -1L if there is no mismatch.

Collections.teeing Streams API gets a new function that applies 2 functions (consumers) on the items and then merges/combines the result of those 2 functions using a third function to produce the final result.

From Javadoc - Returns a Collector that is a composite of two downstream collectors. Every element passed to the resulting collector is processed by both downstream collectors, then their results are merged using the specified merge function into the final result.

字符串方法:缩进(int n),变换(函数f)。

智能投instanceOfcanbeusednowtodoasmartcastasbelow:

...
} catch (Exception ex) {
    if(ex instanceOf InconsistentProcessingStateException ipse) {
        // use ipse directly as InconsistentProcessingStateException
    }
}

JVM改进:具有Shenandoah的低暂停GC,微基准功能,常量API和其他改进。


Java 13 - Released September 17, 2019

多行文字:现在可以定义多行字符串,而无需难看的转义序列\或附加。 例如:

var jsonBody = """
    {
        "name": "Foo",
        "age": 22
    }
""";

这是作为预览引入的,需要-启用预览标记到Javac或在IDE中启用它。

串得到更多的方法,如formatted(),stripIndent()和translateEscapes()用于处理多行文本。

切换表达仍处于预览状态并基于反馈,现在支持: 让除了语法->句法。 因此,我们可以写

String status = process(..., ..., ...);
var isCompleted = switch (status) {
    case "PROCESSED", "COMPLETED": yield true;
    case "WAITING", "STUCK": yield false;
    default: throw new RuntimeException();
};

小号ocket API reimplemented with modern NIO implementation. This is being done to overcome limitations of legacy api and build a better path towards Fiber as part of Project Loom

Z气相色谱改进以释放未使用的内存。


duke thinking
Wow, it's getting crazy out there.
If you are a developer that started a decent size Java project recently in the hopes that you would use the latest features and keep yourself and the project updated with the latest versions, do you feel a pressure to catchup with these frequent releases?

Add your comments below or tweet them to me.

A parting gift - I use Jenv to easily switch between different jdk versions locally while switching between different projects. It's pretty cool to manage multiple jdk versions.

请注意,我在这里谈论的功能是那些可以添加很酷的功能或最大程度提高开发人员生产力的功能。 这是不详尽的清单。


References and good articles:

from: https://dev.to//therajsaxena/java-9-to-java-13-top-features-362l

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值