原文

7 Reasons to Migrate from Java 8 to Java 17 | Level Up Coding (medium.com)

Introduction

From Java 8 to Java 18, Java has come a long way, there have been a lot of changes in the Java ecosystem since Java 8. The most notable change is the release cadence of Java. Java 8 was released in 2014, and Java 17 got released in 2021. That’s a gap of 7 years between the two releases. But now Java is released every six months so that you can expect a new version of Java every six months. This is a massive change for the Java ecosystem, as it allows developers to use the latest features of Java without having to wait for a long time.

从 Java 8 到 Java 18,期间Java生态系统出现了非常多的改变,其中最值得注意是Java的发布节奏转变,Java8 自2014年诞生,Java 17 于 2021年发布,两个版本之间仅仅间隙7年差距。现在Java的发布节奏是每6个月进行一次跨进,你可以发现每一个新版本的周期都是按照6个月的间隔发布。对于Java整个生态系统来说是一次巨变,它使得开发人员不需要等待很长时间就可以用上新版本。

Elevator Pitch

If you are like me, who has been using Java 8 for a long time and feel like you need to catch up on the new features of Java, then this article is for you.

如果你像我一样,已经使用了很长时间的Java 8,并且觉得你需要赶上Java的新功能,那么这篇文章就是为你准备的。

There are way too many features that have been added since Java 8, but not all of them are useful and popular. So I have compiled a list of the most popular features among developers that got added to Java since Java 8 that you can use as a reference to migrate and trick your friends into thinking you are a Java expert.

但是从Java8到Java17发生的变化太多了。因此,我编制了一份列表,列出了自Java 8以来在开发人员中最受欢迎的功能,你可以将其作为迁移的参考,并欺骗你的朋友,让他们认为你是一个Java专家。

Key features

1. Local Variable Type Inference

This is one of the most popular features added to Java since Java 8. It allows you to declare local variables without specifying the type. The type is inferred from the right-hand side of the expression. This feature is also known as var type.

第一个最大的变化是从Java10开始,Java也开始支持不指定类型的情况下声明局部变量,编译器会自动通过右边的内容推导出左边的实际类型。

这一特征也被称为 "var "类型。

image.png

2. Switch expression

When using a switch expression in Java 14, you don’t have to use the break keyword to break out of the switch statement or use return keyword on each switch case to return a value; instead, you can return the entire switch expression. This enhanced switch expression makes the overall code look much cleaner and easier to read.、

当我们在Java14中使用 switch 语句的时候,我们可以不需要写 break 语句选中结果或者在每个case中使用 return 关键字返回分支结果。 作为替代,Java 14 中使用了箭头表达式的方式简化代码书写

image.png

3. Text blocks

Text blocks are a new feature that was added to Java 15. It allows you to create multiline strings without using escape sequences. This is very useful when you are creating SQL queries or JSON strings. In the below example, you can see how much cleaner the code looks when using text blocks.

文本块是 Java 15 新增的新特性,它允许你创建多行文本并且不需要任何转义符,这是一个非常实用的功能,特别是在Java代码中书写一些类似SQL语句或者各种脚本的时候非常有帮助,并且可以看到使用文本快使得代码非常简洁易读。

image.png

4. Records

Records are a new feature that was added to Java 14. It allows you to create classes that are used to store data. It is similar to a POJO class but with much less code; most developers use Lombok to generate POJO classes, but with records, you don’t need to use any third-party libraries. In the below example, you can see how little code is required to create a record class.

【Record】 类 是添加到Java 14中的一个新功能。它允许你创建用于存储数据的类。它类似于POJO类,但代码要少得多;大多数开发者使用Lombok来生成POJO类,但有了【Record】,你不需要使用任何第三方库。在下面的例子中,你可以看到创建一个【Record】所需的代码是多么少。

image.png

5. Pattern matching for instanceof

Pattern matching for instanceof is a new feature that was added to Java 16. It allows you to use the instanceof operator as an expression that returns the casted object. This is very useful when you are working with nested if-else statements. In the below example, you can see how we use the instanceof operator to catch Employee the object, instead of having an explicit cast.

instanceof 的模式匹配是Java 16中增加的一个新功能。它允许你使用instanceof操作符作为一个表达式,返回被匹配的的对象。这种改进在处理嵌套的if-else语句时非常有用。

在下面的例子中,你可以看到我们如何使用instanceof操作符来获取Employee这个对象。

image.png

6. Sealed classes

Sealed classes are a new feature that was added to Java 17. It allows you to restrict the inheritance of a class or interface to a limited set of subclasses. This is very useful when you want to restrict the inheritance of a class or interface to a limited set of subclasses. In the below example, you can see how we use the sealed keyword to restrict the inheritance of a class to a limited set of subclasses.

封闭类是Java 17中加入的一个新特性。它允许你将一个类或接口的继承限制在一组有限的子类中。当你想把一个类或接口的继承权限制在一组有限的子类中时,这非常有用。在下面的例子中,你可以看到我们是如何使用sealed关键字将一个类的继承权限制在一组有限的子类中的。

image.png

The subclass of a sealed class can be declared as final or non-sealed. A final subclass cannot be extended further, whereas a non-sealed subclass can be extended further.
一个密封类的子类可以被声明为final的或非密封的。一个final的子类不能被进一步扩展,而一个非密封的子类可以被进一步扩展。

7. Useful NullPointerException

NullPointerExceptions are a new feature that was added to Java 14. It allows you to get more information about the NullPointerExceptions. This is very useful when you are debugging NullPointerExceptions. In the below example, you can see how the same code generates different NullPointerExceptions in Java 8 and Java 14, but in Java 14, you get more information about the exception

image.png

NullPointerExceptions是Java 14中增加的一个新功能。它允许你获得更多关于NullPointerExceptions的信息。这在你调试NullPointerExceptions时非常有用。在下面的例子中,你可以看到同样的代码在Java 8和Java 14中产生了不同的NullPointerExceptions,但在Java 14中,你可以得到更多的异常信息。

最为实用并且意义很大的功能。

I havent covered all the features that have been added to Java since Java 17, but I have covered the most popular ones. If you want to learn more about the new features of Java, then I would recommend you to check out the links provided below.

上面涵盖了从 Java8 到 Java17 中最受欢迎的新特性,了解更多的新特性,可以参考相关资料:

References


Xander
201 声望53 粉丝