java 未来_Java的新未来

java 未来

There’s always been a considerable level of criticism against Java from a big sector of our industry. This criticism has been mostly focused on Java’s verbosity and the amount of boilerplate code it generates in many cases without need. Although I’ve always liked Java, I couldn’t say that these allegations are wrong. It’s indeed true that Java’s verbosity and its amount of clutter can become very annoying on multiple occasions. However, most of the time, we have to accept that we don’t live in a perfect world, and in most cases, we have to accept the lesser of two evils. So yes, Java wasn’t perfect, we all know that, but the main question is why nothing was done before to address these concerns.

在我们行业中,一直有相当多的人对Java提出批评。 这种批评主要集中在Java的冗长性及其在许多情况下不需要的情况下生成的样板代码的数量。 尽管我一直很喜欢Java,但我不能说这些指控是错误的。 确实,Java的冗长性及其混乱程度在很多情况下会变得非常烦人。 但是,大多数时候,我们必须接受我们没有生活在一个完美的世界中,并且在大多数情况下,我们必须接受两种邪恶中的较小者。 是的,Java并不是完美的,我们都知道,但是主要的问题是为什么以前没有做任何事情来解决这些问题。

In my personal opinion, the only reason why changes took so long is that there wasn’t enough competition for Java, and things were right as they were. The Java language was dominating the market probably due to the lack of serious competitors and the big efforts made by Sun first and then Oracle afterward.

以我个人的观点,更改花了这么长时间的唯一原因是,对于Java的竞争不够,而且事情是正确的。 Java语言在市场上占主导地位,这可能是由于缺乏认真的竞争者以及Sun首先,然后是Oracle的巨大努力。

The strong type safety provided by Java and some of its characteristics as a well-structured language made it a very popular language for big projects. Using Java, it’s generally more difficult for things to get really out of hand. Also, one of the main characteristics of Java — that it’s a multi-platform language that runs on its own virtual machine — made it a perfect match for many organisations. If you add its inherent capability of performing automatic performance optimisations through its famous JIT compiler, something that minimises in many cases the effects of badly written code, then you have a pretty solid set of reasons to use Java.

Java提供的强类型安全性以及它作为一种结构良好的语言的某些特性使其成为大型项目中非常流行的语言。 使用Java,通常很难使事情真正失控。 另外,Java的主要特征之一-它是一种在其自己的虚拟机上运行的多平台语言-使其成为许多组织的完美选择。 如果您通过其著名的JIT编译器添加了其执行自动性能优化的固有功能(在许多情况下,这些性能可以最大限度地减少不良代码的影响),那么您就有使用Java的充分理由。

But then what happened? What happened is that new languages able to run in the same JVM as Java were introduced into the market, languages that were eliminating some of the biggest annoyances in Java and offering a nicer environment to developers with quite a flat learning curve, in some cases.

但是后来发生了什么? 发生的事情是,市场上引入了能够在与Java相同的JVM上运行的新语言,这些语言消除了Java中的一些最大烦恼,并在某些情况下为开发人员提供了更好的环境,使其学习曲线相当平坦。

Before we continue, let’s recapitulate and take a brief tour through the history of JVM languages.

在继续之前,让我们简要回顾一下,并简要介绍一下JVM语言的历史。

JVM语言的历史 (History of JVM Languages)

One thing I’d like to clarify before we start is that I’ve skipped some of the existing JVM languages mainly because they never got enough traction to be considered candidates for wide use in our industry. Having said that, let’s kick off our quick tour through the history of JVM languages.

在开始之前,我想澄清的一件事是,我跳过了一些现有的JVM语言,主要是因为它们没有足够的吸引力被认为是我们行业中广泛使用的候选语言。 话虽如此,让我们开始快速浏览JVM语言的历史。

Java
Photo by Maximilian Weisbecker on Unsplash
Maximilian WeisbeckerUnsplash上的 照片

We have to start with Java, of course, the oldest and most popular language in the JVM world.

我们必须从Java开始,当然Java是JVM世界中最古老,最流行的语言。

Java was officially released in January 1996, so it’s been around for twenty-four years already; not bad, eh? Initially, Java was a purely imperative language following a purely object-oriented programming style; it was also a strongly typed language. Java’s syntax is similar in some ways to C++ and C languages, but it’s considered an improved version because it’s much easier to write code with Java than C or C++. On the other hand, we have that the biggest argument among its detractors is its verbosity.

Java于1996年1月正式发布,因此已经存在了24年。 还不错吧? 最初,Java是遵循纯粹的面向对象编程风格的纯粹命令式语言。 它也是一种强类型语言。 Java的语法在某些方面类似于C ++C语言,但是它被认为是改进的版本,因为与Java或C ++相比,用Java编写代码要容易得多。 另一方面,我们有一个最大的争议是它的冗长。

The second JVM language that was released was Groovy. It’s been around since 2003, although its first official and standardised version 1.0 didn’t come up until January 2007. Groovy had the benefit of being able to be used as a scripting language as well. Groovy is a dynamically typed language, so type checks occur at runtime; this is one of the reasons why some developers don’t like Groovy. You write your code in Groovy, and it looks correct at compilation time, but then at runtime, you realise there’s something wrong.

发布的第二种JVM语言是Groovy 。 它自2003年以来一直存在,尽管它的第一个正式和标准化版本1.0直到2007年1月才出现。Groovy的优势在于它还可以用作脚本语言。 Groovy是一种动态类型化的语言,因此类型检查会在运行时进行; 这是一些开发人员不喜欢Groovy的原因之一。 您用Groovy编写代码,在编译时看起来是正确的,但是在运行时,您发现有问题。

Groovy
Source: Wikipedia
资料来源:维基百科

A very popular language for a few years came up next. We’re talking about Scala. Scala was officially released in 2004, and it brought a new programming model to the JVM world, with functional programming and its declarative approach. We could affirm that Scala was the first to bring this concept of immutability that we’ve talked about before, so it has done very good things for Java transformation. On the other hand, Scala detractors don’t like it due to its complex grammar and general low readability.

接下来出现了几年流行的语言。 我们在谈论Scala 。 Scala于2004年正式发布,它通过函数式编程及其声明性方法为JVM世界带来了新的编程模型。 我们可以肯定,Scala是第一个提出我们之前讨论的不变性概念的人,因此它为Java转换做了非常好的事情。 另一方面,Scala批评者不喜欢它,因为它语法复杂且可读性一般较低。

Scala
Source: Wikipedia
资料来源:维基百科
Clojure language
Source: Wikipedia
资料来源:维基百科

The next language to make its appearance in the JVM world was Clojure, a purely functional language. It’s become quite popular recently, but it first appeared in 2007. Clojure is a LISP-based language characterised by its simplicity and the use of pure functions. Among its drawbacks, we could mention that it’s dynamically typed (same as Groovy) and the learning curve is much steeper, as its syntax is completely different from other JVM languages.

在JVM世界中出现的下一种语言是Clojure ,一种纯粹的功能性语言。 最近变得很流行,但是 首次出现在2007年。Clojure是一种基于LISP的语言,其特征在于其简单性和纯函数的使用。 在它的缺点中,我们可以提到它是动态类型的(与Groovy相同),并且学习语法要陡峭得多,因为它的语法与其他JVM语言完全不同。

There are a couple of good books that you should read if you’re interested in learning Clojure: “The Joy of Clojure” and “Programming Clojure: 3rd Edition.”

如果您对学习Clojure有兴趣,应该阅读几本好书:“ Clojure 的喜悦 ”和“ Clojure编程:第三版 ”。

And last but not least, we have Kotlin. Kotlin was first released in February 2016, and since then, it hasn’t stopped growing in popularity. It was designed by the company JetBrains with one clear goal: eliminate all the most popular Java problems. In the way it’s been designed, it has kept all the good things about Java but at the same time eliminated the majority of its problems. That’s why it’s becoming so popular and many consider that it could defeat Java in the next few years.

最后但并非最不重要的一点是,我们有Kotlin 。 Kotlin于2016年2月首次发布,从那以后,它的流行就一直没有停止。 它由JetBrains公司设计,目的很明确:消除所有最流行的Java问题。 在设计方式上,它保留了Java的所有优点,但同时消除了大多数问题。 这就是为什么它如此流行并且许多人认为它可能在未来几年内击败Java。

If you want to know more about Kotlin (I encourage you to do so, as it’s a great language) I’d recommend that you read the book “Kotlin in Action,” a fantastic book for Java developers starting in Kotlin.

如果您想了解有关Kotlin的更多信息(我鼓励您这样做,因为它是一种很棒的语言),我建议您阅读本书“ Kotlin in Action ”,这是一本面向Kotlin的Java开发人员的绝妙书籍。

Kotlin
Photo by Louis Tsai on Unsplash
路易·蔡Unsplash上的 照片

These are the most important JVM languages. We have skipped some others that are not so popular, but we could mention some of them: Jython, JRuby, Ceylon, Fantom, etc. You can check out the whole list of the existing JVM languages on Wikipedia.

这些是最重要的JVM语言。 我们已经跳过了一些不太流行的其他语言,但是我们可以提及其中的一些:Jython,JRuby,Ceylon,Fantom等。您可以在Wikipedia上查看现有JVM语言的完整列表

One thing that we notice is that Java didn’t have many competitors in the first eight or ten years, but since then, it’s started having some competition. So is having competition good or bad?

我们注意到的一件事是Java在最初的八到十年没有很多竞争对手,但是从那时起,它开始出现一些竞争。 那么竞争是好是坏?

竞争加剧的好处 (Benefits of Increased Competition)

As we mentioned earlier, Java hadn’t changed too much in its early years probably because there was no need for it. The language was widely used and very popular despite being far from perfect.

正如我们前面提到的,Java在其早期并没有太大改变,可能是因为它不需要它。 尽管还不够完善,但该语言仍被广泛使用并非常受欢迎。

But then new competitors showed up, more modern languages that were bringing new features and solving some of the pain points that Java developers had been suffering for a long time.

但是随后出现了新的竞争对手,更多的现代语言带来了新功能并解决了Java开发人员长期以来遭受的某些痛苦。

For instance, let’s look at the Scala language. Scala’s popularity was increasing since 2009. Developers were welcoming this new functional style that allowed them more flexibility and also the ability to write parallel code safely and easily. We can see this trend in the following Google Trends graph:

例如,让我们看一下Scala语言。 自2009年以来,Scala的受欢迎程度一直在增加。开发人员欢迎这种新的功能样式,它使他们拥有更大的灵活性,并且能够安全,轻松地编写并行代码。 我们可以在下面的Google趋势图中看到这种趋势:

Image for post
Source: theboreddev.com
资料来源:theboreddev.com

What was Oracle’s reaction to this new trend? Java Lambdas and Streams were released in 2014. I think we can all agree that was when Java made its biggest step to defeat Scala. Everyone who currently works in the industry can feel the loss of steam that Scala has been suffering in recent years.

Oracle对这一新趋势有何React? Java Lambdas和Streams于2014年发布。我认为我们都同意那是Java击败Scala迈出的最大一步。 目前从事该行业的每个人都可以感受到Scala近年来遭受的损失。

One more benefit in having more competitors in the JVM world is the continuous improvements that the JIT compiler and the JVM itself are experiencing. Now there are many more people interested in having an optimised JVM and improved performance optimisations. So competition has been a good thing for everyone!

在JVM世界中拥有更多竞争对手的另一个好处是JIT编译器和JVM本身正在不断改进。 现在,有更多的人对具有优化的JVM和改进的性能优化感兴趣。 因此,竞争对每个人都是一件好事!

The last competitor to appear in the arena has been Kotlin, and Kotlin has been very important because, in some ways, it has shown Oracle the way to go forward. Kotlin has shown that it’s possible to keep the good parts of Java but create a more concise language that’s faster to code.

最后一次出现在竞技场上的竞争对手是Kotlin,而Kotlin之所以非常重要,是因为它在某种程度上为Oracle指明了前进的方向。 Kotlin表明,可以保留Java的大部分内容,但可以创建一种更简洁的语言,以更快地进行编码。

If we look at Google Trends to see how popular Kotlin has been in the last few years, this is what we have:

如果我们查看Google趋势以了解Kotlin在过去几年中的流行程度,那就是:

Image for post
Source: theboreddev.com
资料来源:theboreddev.com

It seems that Kotlin became quite popular fairly quickly. However, it appears to have been settling down in the last couple of years.

看来Kotlin很快就很流行。 但是,它似乎在最近几年已经稳定下来。

Oracle has taken good note of the industry’s reactions with regards to Kotlin. If you take a look at the JDK 15 release notes, you can see that some of Java's new features are basically copies of what Kotlin had changed. Examples include the new Java records, the new text blocks (multi-line string with triple quotes), and the new switch statement, which is more or less a copy of the Kotlin when statement.

Oracle很好地注意到了业界对Kotlin的React。 如果看一下JDK 15发行说明 ,您会发现Java的一些新功能基本上是Kotlin所做更改的副本。 示例包括新的Java记录 ,新的文本块(带三引号的多行字符串)和新的switch语句,后者或多或少是Kotlin when语句的副本。

All of this we’ve talked about is what I call “The Kotlinisation of Java.” By becoming a stronger competitor, Kotlin has shown Java the way to go. From my point of view, Kotlin has been the only language I’ve seen that could overcome Java as a leader in our industry.

我们谈论的所有这些就是我所说的“ Java的Kotlinisation”。 通过成为更强大的竞争对手,Kotlin为Java指明了前进的道路。 从我的角度来看,Kotlin是我所见过的唯一可以超越Java成为我们行业领导者的语言。

Java的“科特琳化” (The “Kotlinisation” of Java)

Some of the upcoming Java features are going to be a massive improvement for Java in terms of readability and in terms of improving one of the main weaknesses in Java, its verbosity. We could affirm that they all have a suspicious similarity to some Kotlin features.

就可读性和改善Java的主要弱点之一(即冗长性)而言,一些即将到来的Java功能将对Java进行重大改进。 我们可以肯定它们都与Kotlin的某些功能具有可疑的相似性。

Please keep in mind that most of these features are feature previews. What that means is that if you install JDK 14 or JDK 15 when it gets released, you won’t be able to use them by default.

请记住,这些功能大多数都是功能预览 。 这意味着,如果在发布JDK 14或JDK 15时安装它们,则默认情况下将无法使用它们。

Java feature previews are new features that are introduced in a release but are disabled by default. They’re included in the release just to gather feedback from the community of developers, so they’re still subject to changes. That’s why it’s not recommended to use them in production code.

Java功能预览是发行版中引入的新功能,但默认情况下处于禁用状态。 它们包含在发行版中只是为了收集开发人员社区的反馈,因此它们仍可能会发生更改。 这就是为什么不建议在生产代码中使用它们的原因。

To enable them at compilation time, you’d have to do the following:

要在编译时启用它们,您必须执行以下操作:

javac --enable-preview --release 14

If you want to enable them at runtime, you’d have to run the following:

如果要在运行时启用它们,则必须运行以下命令:

java --enable-preview YourClass

Of course, you can also enable them in your IDE, but be careful to not enable previews by default in all your new projects!

当然,您也可以在IDE中启用它们,但是要注意不要在所有新项目中默认启用预览!

Let’s take a look at those changes that are going to have a bigger impact on our coding with future versions of Java.

让我们看一下那些对将来的Java版本的编码产生更大影响的更改。

Java记录 (Java records)

Java records are a feature that many of us have been demanding for a long time. I guess you’ve been in the same situation multiple times, when you reluctantly have to implement toString, hashCode, equals , and also getters for each existing field. (I’m assuming that you’re no longer using setters, and you definitely should not.)

Java记录是我们许多人长期以来一直要求的功能。 我猜想您多次遇到相同的情况,而您无奈地必须为每个现有字段实现toStringhashCodeequals和getter。 (我假设您不再使用setter,并且您绝对不应该使用。)

Kotlin provides data classes to solve this problem, and Java is intending to do the same by releasing the record classes, something that Scala also has with its case classes.

Kotlin提供了数据类来解决此问题,Java打算通过发布记录类来做到这一点,Scala的case类也具有这种功能

The main purpose of these classes is to hold immutable data in an object. Let’s take a look at how much better will it be in Java by looking at an example. This is how much code we’d have to write to be able to instantiate and compare our Employee class:

这些类的主要目的是将不可变数据保存在对象中。 让我们看一个例子,看看它在Java中会有多好的发展。 这是我们必须编写多少代码才能实例化和比较Employee类:

And also the Address object it contains:

还有它包含的Address对象:

That’s a lot of code for something so simple, isn’t it?

这么简单的东西需要很多代码,不是吗?

Let’s take a look now at how it will look with the new Java records:

现在让我们看一下新的Java记录的外观:

And also our Address class again:

还有我们的Address类:

This is exactly the same we wrote earlier with so much code. You will have to agree with me on this: This is amazing, the amount of code we’re going to save and how much simpler it is!

这与我们之前编写的大量代码完全相同。 您将在以下方面与我达成一致:这太了不起了,我们将要节省的代码量以及它的简单程度!

Let’s see now what the differences are in the new switch statement.

现在让我们看看新的switch语句有什么区别。

改进了switch语句 (Improved switch statement)

The new switch statements in Java solve some of the inherent problems of using switch statements in Java. Switch statements at this moment should always be avoided because they’re very error-prone and lead to code duplication. Currently, it’s very easy to leave a case uncovered, for example.With the new switch statement, that problem is solved because it won’t compile if our switch statement doesn’t cover the whole range of the domain that belongs to the type we pass in to the switch.

Java中的新switch语句解决了在Java中使用switch语句的一些固有问题。 此时应始终避免使用switch语句,因为它们很容易出错,并且会导致代码重复。 例如,当前很容易发现一个案例,使用新的switch语句解决了该问题,因为如果我们的switch语句不能覆盖属于我们类型的域的整个范围,它将无法编译传递到交换机。

To explain this with an example, we’re going to create a DayOfTheWeek enum in Java:

为了举例说明,我们将在Java中创建DayOfTheWeek枚举:

Having that, our switch is going to tell us what position in the week corresponds to that day. Let’s see first how can we currently do this using Java 11.

有了这个,我们的开关将告诉我们一周中对应于当天的位置。 首先让我们看一下我们现在如何使用Java 11做到这一点。

Using the current switch statement, we’d have to make use of a variable, and also, if we miss one of the days of the week, our code will compile perfectly. This is one of the problems of switch statements: It’s very error-prone.

使用当前的switch语句,我们必须使用一个变量,并且,如果我们错过一周中的某一天,我们的代码将完美地编译。 这是switch语句的问题之一:它非常容易出错。

So how does Java 14 improve the situation? Let’s quickly see how:

那么Java 14如何改善这种情况呢? 让我们快速看看如何:

You will be able to see very quickly that the new switch statements can be used as an expression, not only as a statement.

您将很快看到新的switch语句不仅可以用作语句,还可以用作表达式。

The result is more concise and expressive. That would be enough to convince many of us to use them, but one of the main improvements is that now switch statements won’t compile if we don’t cover all the cases in our switch. It will show us the following error, for example:

结果更加简洁和富有表现力。 这足以说服我们许多人使用它们,但是主要的改进之一是,如果我们不涵盖切换中的所有情况,现在switch语句将不会编译。 例如,它将显示以下错误:

Error:(9, 24) java: the switch expression does not cover all possible input values

From now on, it’ll be impossible to miss a case in our switch statements. That’s awesome, isn’t it?

从现在开始,将不可能在我们的switch语句中错过一个案例。 太棒了,不是吗?

This is very similar to Kotlin when statements, which you can read about in the documentation.

这与Kotlin when语句非常相似,您可以在文档中阅读有关内容

Let’s also take a look at the new text blocks.

让我们也看看新的文本块。

文字区块 (Text blocks)

Have you ever moaned about how ugly and difficult it was to assign a big blob of JSON to a variable in Java? Java will introduce multi-line strings that you can define by wrapping them between triple quotes. When this feature gets officially released, it’ll be much easier to define long strings in multiple lines.

您是否曾经抱怨过将JSON大块分配给Java中的变量有多么丑陋和困难? Java将引入多行字符串,您可以通过在三引号之间包裹多行字符串来定义它们。 正式发布此功能后,在多行中定义长字符串会容易得多。

Let’s take a look at the differences between the two modes. At the moment, if we want to store a formatted JSON in a variable, it looks as ugly as this:

让我们看一下两种模式之间的区别。 目前,如果我们要将格式化的JSON存储在变量中,它看起来像这样丑陋:

On the other hand, when new text blocks get released, it’ll be as easy and clean as this:

另一方面,当释放新的文本块时,它将像下面这样简单而干净:

I think that’s much better, don’t you agree? This is also something supported in Kotlin, as you can find in its type definitions.

我认为这要好得多,您不同意吗? 您还可以在Kotlin的类型定义中找到这点。

So we’ve seen that Java is “inheriting” many of the solutions to its own problems from one of its competitors: Kotlin. We don’t know if this time Oracle has reacted right in time to combat the rise of Kotlin or if maybe it comes too late. Personally, I think that Java is taking the right steps forward, even if these changes were triggered in some way by its competitors and might come a bit late.

因此,我们已经看到Java从其竞争对手之一Kotlin中“继承”了许多解决其自身问题的解决方案。 我们不知道这一次Oracle是否及时做出了正确的React来应对Kotlin的崛起,或者为时已晚。 就个人而言,我认为Java正在朝着正确的方向迈进,即使这些变化是其竞争对手以某种方式触发的,也可能会来得晚。

As mentioned earlier, if this article has triggered your interest in learning the Kotlin language, I’d recommend that you read “Kotlin in Action,” a very good book for Java developers starting in Kotlin.

如前所述,如果本文激发了您学习Kotlin语言的兴趣,我建议您阅读“ Kotlin in Action ”,这是一本非常适合从Kotlin开始的Java开发人员的书。

结论 (Conclusion)

I think competition has been the best thing that has ever happened to the Java language. I have the impression that otherwise, Java would have rested on its laurels. Also, Java competitors have shown that a different way of programming was possible, showing the way to go forward and avoid staying in an old-fashioned and rusty way of writing code.

我认为竞争一直是Java语言发生过的最好的事情。 我给人的印象是,否则,Java将会获得桂冠。 而且,Java竞争者表明,可以采用另一种编程方式,这表明了前进的方向,并避免停留在老式且生锈的代码编写方式中。

The changes I’ve been seeing in Java recently, with all the upcoming features and improvements, are making a stronger Java than ever, a language adapted to the current times, a language that wants to evolve and forget about the traditional way of doing things: a new future for Java!

我最近在Java中所看到的变化,以及所有即将到来的功能和改进,正在使Java变得比以往任何时候都更强大,一种适应当前时代的语言,一种想要发展并忘记传统做事方式的语言:Java的新未来!

I really hope you’ve enjoyed this reading, and I hope I can see you soon again.

我真的希望您喜欢阅读,也希望很快能再见到您。

Thank you very much for reading!

非常感谢您的阅读!

翻译自: https://medium.com/better-programming/a-new-future-for-java-b10a6789f962

java 未来

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值