原文
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 “ 类型。
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 中 应用了箭头表达式的形式简化代码书写。
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 语句或者各种脚本的时候十分有帮忙,并且能够看到应用文本快使得代码十分简洁易读。
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】所需的代码是如许少。
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
这个对象。
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 关键字将一个类的继承权限度在一组无限的子类中的。
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
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
- JDK 10 features — https://openjdk.org/projects/jdk/10/
- JDK 11 features — https://openjdk.org/projects/jdk/11
- JDK 12 features — https://openjdk.org/projects/jdk/12
- JDK 13 features — https://openjdk.org/projects/jdk/13
- JDK 14 features — https://openjdk.org/projects/jdk/14
- JDK 15 features — https://openjdk.org/projects/jdk/15
- JDK 16 features — https://openjdk.org/projects/jdk/16
- JDK 17 features — https://openjdk.org/projects/jdk/17