您可以使用本指南获取有关如何查找和安装最新Java的实用信息,了解Java发行版之间的差异(AdoptOpenJdk,OpenJDK,OracleJDK等),以及Java语言功能的概述,包括Java版本 8-13。
Practical Information
首先,让我们看看人们在尝试为其项目选择正确的Java版本时遇到的一些常见的实际问题。
TL;DR I only want a download link and know about everything else. Where should I go?
Go to the AdoptOpenJDK site, chose the latest Java version, download and install it. Then come back to this guide to maybe still learn a thing or two about Java versions.
What Java version should I use?
As of September 2019, Java 13 is the latest released Java version, with newer versions following every 6 months. Hence Java 14 is scheduled for March 2020, Java 15 for September 2020 and so on. In the past, Java release cycles were much longer, up to 3-5 years!.
随着许多新版本的问世,基本上存在以下实际使用情况:
Legacy projects in companies are often stuck with using Java 8 (see Why are companies still stuck with Java 8?) . Hence you will be forced to use Java 8 as well.
Some legacy projects are even stuck on Java 1.5 (released 2004) or 1.6 (released 2006) - sorry, pals!
If you are making sure to use the very latest IDEs, frameworks and build tools and starting a greenfield project, you can, without hesitation, use Java 11 (LTS) or even the latest Java 13.
There’s the special field of Android development, where the Java version is basically stuck at Java 7, with a specific set of Java 8 features available. Or you switch to using the Kotlin programming language.
Why are companies still stuck with Java 8?
公司仍然坚持使用Java 8的原因有多种,有多种:
Build tools (Maven, Gradle etc.) and some libraries initially had bugs with versions Java versions > 8 and needed updates. Even today, with e.g. Java >=9, certain build tools print out "reflective access"-warnings when building Java projects, which simply "feels not ready", even though the builds are fine.
Up until Java 8 you were pretty much using Oracle’s JDK builds and you did not have to care about licensing. Oracle changed the licensing scheme In 2019, though, which led the internet go crazy with a ton of articles saying "Java is not free anymore" - and a fair amount of confusion followed. This is however not really an issue, which you’ll learn about in the Java Distributions section of this guide.
Some companies have policies to only use LTS versions and rely on their OS vendors to provide them these builds, which takes time.
总结一下:您遇到了许多实际问题(升级工具,库,框架)和政治问题。
Why are some Java versions, like 8 also called 1.8?
9之前的Java版本仅具有不同的命名方案。 因此,Java 8也可以称为1.8,Java 5可以称为1.5等等。当您发出'java -version'命令时,使用这些版本,您将得到如下输出:
- c:\Program Files\Java\jdk1.8.0_191\bin>java -version
- java version "1.8.0_191"
- Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
- Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
这简直就是Java8。随着Java 9切换到基于时间的发行版,命名方案也发生了变化,并且Java版本不再以1.x作为前缀。 现在,版本号如下所示:
- c:\Program Files\Java\jdk11\bin>java -version
- openjdk version "11" 2018-09-25
- OpenJDK Runtime Environment 18.9 (build 11+28)
- OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
What is the difference between the Java versions? Should I learn a specific one?
来自其他发行版本之间的重大差异的其他编程语言,例如Python 2至3,您可能想知道是否同样适用于Java。
Java在这方面很特别,因为它是非常向后兼容。 这意味着您的Java 5或8程序可以保证与Java 8-13虚拟机一起运行-除了一些您现在无需担心的例外。
例如,您的程序依赖于Java 13功能,而Java 8 JVM根本不提供该功能,显然,它反之亦然。
这意味着两件事:
- 您不仅可以“学习”特定的Java版本,例如12。相反,您将在Java 8之前的所有语言功能中都有良好的基础。这是一个良好的基础。然后从这样的指南中了解附加功能Java 9-13中引入了它,并尽可能使用它们。
What are examples of these new features between Java versions?
Have a look at the Java Features 8-13 section.
但根据经验:较旧,较长的发行周期(3-5年,直到Java 8)意味着每个发行版都有许多新功能。
6个月的发布周期意味着每个版本的功能要少得多,因此您可以快速掌握Java 9-13语言功能。
What is the difference between a JRE and a JDK?
到目前为止,我们仅谈论“ Java”。 但是Java到底是什么?
首先,您需要区分杰瑞(Java运行时环境)和一个JDK(Java开发套件)。
从历史上看,您下载的只是杰瑞 if you were only interested in running Java programs. A 杰瑞 includes, among other things, the Java Virtual Machine (JVM) and the "java" command line tool.
要开发新的Java程序,您需要下载一个JDK。 一个JDK包括一切JRE以及编译器Java语言以及其他一些工具,例如Java文档(Java文档生成器)和数据库(Java调试器)。
现在为什么我用过去时说话呢?
直到Java 8为止,Oracle网站都将JRE和JDK作为单独的下载提供-尽管JDK始终始终在单独的文件夹中包含JRE。 在Java 9中,这种区别基本上消失了,总是下载JDK。 JDK的目录结构也已更改,不再有显式的JRE文件夹。
So, even though some distributions (see Java Distributions section) still offer a separate JRE download, there seems to be the trend of offering just a JDK. Hence, we are going to use Java and JDK interchangeably from now on.
How do I install Java or a JDK then?
暂时忽略Java-Docker映像,.msi包装器或特定于平台的软件包。 最后,Java只是一个.zip文件,仅此而已。
因此,将Java安装到计算机上所需要做的就是解压缩jdk- {5-13} .zip文件。 您甚至不需要管理员权限。
您解压缩后的Java文件如下所示:
- Directory C:\dev\jdk-11
-
- 12.11.2019 19:24 <DIR> .
- 12.11.2019 19:24 <DIR> ..
- 12.11.2019 19:23 <DIR> bin
- 12.11.2019 19:23 <DIR> conf
- 12.11.2019 19:24 <DIR> include
- 12.11.2019 19:24 <DIR> jmods
- 22.08.2018 19:18 <DIR> legal
- 12.11.2019 19:24 <DIR> lib
- 12.11.2019 19:23 1.238 release
魔术发生在/ bin目录中,在Windows下看起来像这样:
- Directory C:\dev\jdk-11\bin
- ...
- 12.11.2019 19:23 272.736 java.exe
- ...
- 12.11.2019 19:23 20.832 javac.exe
- ...
因此,您所需要做的就是解压缩该文件并将/ bin目录放在PATH变量中,以便您可以从任何地方调用'java'命令。
(如果您想知道,GUI安装程序(例如来自Oracle或AdoptOpenJDK的GUI安装程序)将为您解压缩并修改PATH变量。
要验证您是否正确安装了Java,则只需运行“ java -version”。 如果输出看起来像下面的输出,那就很好了。
- openjdk version "11" 2018-09-25
- OpenJDK Runtime Environment 18.9 (build 11+28)
- OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
现在剩下一个问题:您从哪里获得Java .zip文件? 这将我们带到了发行主题。
Java Distributions
有各种各样的网站提供Java下载(阅读:JDK),目前尚不清楚“谁提供什么许可以及哪种许可”。 本节将对此进行一些说明。
The OpenJDK project
In terms of Java source code (read: the source code for your JRE/JDK), there is only one, living at the OpenJDK project site.
但是,这只是源代码,而不是可分发的内部版本(请考虑:针对特定操作系统的带有已编译Java命令的。zip文件)。 从理论上讲,您和我可以从该源代码生成一个构建,称其为“马可·杰克并开始分发它。 但是我们的发行商将缺乏认证,无法合法地称呼自己兼容Java SE。
That’s why in practice, there’s a handful of vendors that actually create these builds, get them certified (see ŤCK) and then distribute them.
而且,尽管供应商无法在生成新的Java版本之前从String类中删除方法,但他们可以添加商标(是!)或添加他们认为有用的其他实用程序(例如CLI)。 但是除此之外,原始的源代码是相同对于所有Java发行版。
OpenJDK builds (by Oracle) and OracleJDK builds
从源代码构建Java的供应商之一是Oracle。 这导致两种不同的Java发行版,一开始可能会很混乱。
OpenJDK builds by Oracle(!). These builds are free and unbranded, but Oracle won’t release updates for older versions, say Java 13, as soon as Java 14 comes out.
OracleJDK, which is a branded, commercial build starting with the license change in 2019. Which means it can be used for free during development, but you need to pay Oracle if using it in production. For this, you get longer support, i.e. updates to versions and a telephone number you can call if your JVM goes crazy.
Now, historically (pre-Java 8) there were actual source differences between OpenJDK builds and OracleJDK builds, where you could say that OracleJDK was 'better'. But as of today, both versions are essentially the same, with minor differences.
然后归结为您想要安装的Java版本的付费商业支持(电话号码)。
AdoptOpenJDK
In 2017, a group of Java User Group members, developers and vendors (Amazon, Microsoft, Pivotal, Redhat and others) started a community, called AdoptOpenJDK.
They provide free, rock-solid OpenJDK builds with longer availibility/updates and even offer you the choice of two different Java virtual machines: HotSpot and OpenJ9.
强烈推荐如果您要安装Java。
Azul Zulu, Amazon Corretto, SAPMachine
You will find a complete list of OpenJDK builds at the OpenJDK Wikipedia site. Among them are Azul Zulu, Amazon Corretto as well as SapMachine, to name a few. To oversimplify it boils down to you having different support options/maintenance guarantees.
但是,请务必查看各个网站,以了解每种发行版本的优势。
Recommendation
To re-iterate from the beginning, in 2019, unless you have very specific requirements, go get your jdk.zip (.tar.gz/.msi/.pkg) file from https://adoptopenjdk.net or choose a package provided by your OS-vendor.
Java Features 8-13
如本指南开头所述:本质上所有 (don’t be picky now) Java 8 language features also work in Java 13. The same goes for 所有 other Java versions in between.
反过来,这意味着Java 8的所有语言功能都可以作为非常好的Java基础知识,而其他所有功能(Java 9-13)都是很多附加功能在上面该基准。
以下是特定版本所提供内容的简要概述:
- Java 8 -
Java 8 was a massive release and you can find a list of all features at the Oracle website. There’s two main feature sets I’d like to mention here, though:
Language Features: Lambdas etc.
在Java 8之前,无论何时要实例化(例如)新的Runnable,都必须编写一个匿名内部类,如下所示:
- Runnable runnable = new Runnable(){
- @Override
- public void run(){
- System.out.println("Hello world !");
- }
- };
对于lambda,相同的代码如下所示:
Runnable runnable = () -> System.out.println("Hello world two!");
您还获得了方法参考,重复注释,接口的默认方法以及其他一些语言功能。
Collections & Streams
在Java 8中,您还获得了集合的函数式操作,也称为Stream API。 一个简单的例子:
List<String> list = Arrays.asList("franz", "ferdinand", "fiel", "vom", "pferd");
现在,在Java 8之前的版本中,您基本上必须编写for循环才能对该列表进行处理。
使用Streams API,您可以执行以下操作:
- list.stream()
- .filter(name -> name.startsWith("f"))
- .map(String::toUpperCase)
- .sorted()
- .forEach(System.out::println);
If you want more Java 8 practice
显然,在本指南的范围内,我只能简要概述Java 8中每个新添加的Stream,Lambda或Optional方法。
If you want a more detailed, thorough overview - including exercises - you can have a look at my Java 8 core features course.
- Java 9 -
Java 9也是一个相当大的发行版,其中增加了一些功能:
Collections
集合有几个新的帮助器方法,可以轻松地构造列表,集合和地图。
- List<String> list = List.of("one", "two", "three");
- Set<String> set = Set.of("one", "two", "three");
- Map<String, String> map = Map.of("foo", "one", "bar", "two");
Streams
Streams以takeWhile,dropWhile,iterate方法的形式添加了一些附加功能。
- Stream<String> stream = Stream.iterate("", s -> s + "s")
- .takeWhile(s -> s.length() < 10);
Optionals
Optionals严重错过了ifPresentOrElse方法。
user.ifPresentOrElse(this::displayAccount, this::displayLogin);
Interfaces
接口有私有方法:
- public interface MyInterface {
-
- private static void myPrivateMethod(){
- System.out.println("Yay, I am private!");
- }
- }
Other Language Features
还有其他一些改进,例如改进的try-with-resources语句或菱形运算符扩展。
JShell
最后,Java提供了一个Shell,您可以在其中试用简单的命令并立即获得结果。
- % jshell
- | Welcome to JShell -- Version 9
- | For an introduction type: /help intro
-
- jshell> int x = 10
- x ==> 10
HTTPClient
Java 9带来了新HttpClient的初始预览版。 直到那时,Java的内置Http支持还只是底层的,您只能依靠Apache HttpClient或OkHttp之类的第三方库(它们是很棒的库,btw!)。
使用Java 9,Java有了自己的现代客户端-尽管处于预览模式,这意味着在更高版本的Java中可能会有所更改。
Project Jigsaw: Java Modules and Multi-Release Jar Files
Java 9 got the Ĵigsaw Module System, which somewhat resembles the good old OSGI specification. It is not in the scope of this guide to go into full detail on Jigsaw, but have a look at the previous links to learn more.
多版本.jar文件使一个.jar文件成为可能,该文件包含针对不同JVM版本的不同类。 因此,例如,在Java 8与Java 10上运行时,您的程序可以表现不同/具有不同的类。
If you want more Java 9 practice
Again, this is just a quick overview of Java 9 features and if you want more thorough explanations and exercises, have a look at the Java 9 core features course.
- Java 10 -
Java 10进行了一些更改,例如Garbage Collection等。但是,作为开发人员,您唯一可能真正看到的更改就是引入了“ var”关键字,也称为局部变量类型推断。
Local-Variable Type Inference: var-keyword
- // Pre-Java 10
-
- String myName = "Marco";
-
- // With Java 10
-
- var myName = "Marco"
Feels Javascript-y, doesn’t it? It is still strongly typed, though, and only applies to variables inside methods (thanks, dpash, for pointing that out again).
- Java 11 -
从开发人员的角度来看,Java 11的发行版也较小。
Strings & Files
字符串和文件有一些新方法(此处未列出所有方法):
- "Marco".isBlank();
- "Mar\nco".lines();
- "Marco ".strip();
-
- Path path = Files.writeString(Files.createTempFile("helloworld", ".txt"), "Hi, my name is!");
- String s = Files.readString(path);
Run Source Files
从Java 10开始,您可以运行Java源文件没有必须先编译它们。 迈向脚本编写的一步。
ubuntu@DESKTOP-168M0IF:~$ java MyScript.java
Local-Variable Type Inference (var) for lambda parameters
标头说明了一切:
(var firstName, var lastName) -> firstName + lastName
HttpClient
Java 9中的HttpClient最终的非预览版。
Other stuff
飞行记录器,无操作垃圾收集器,已弃用Nashorn-Javascript-Engine等
- Java 12 -
Java 12 got a couple new features and clean-ups, but the only ones worth mentioning here are Unicode 11 support and a preview of the new switch expression, which you will see covered in the next section.
- Java 13 -
You can find a complete feature list here, but essentially you are getting Unicode 12.1 support, as well as two new or improved preview features (subject to change in the future):
Switch Expression (Preview)
开关表达式现在可以返回一个值。 而且您可以对表达式使用lambda样式的语法,而不会出现直通/突破问题:
旧的switch语句如下所示:
- switch(status) {
- case SUBSCRIBER:
- // code block
- break;
- case FREE_TRIAL:
- // code block
- break;
- default:
- // code block
- }
而在Java 13中,switch语句如下所示:
- boolean result = switch (status) {
- case SUBSCRIBER -> true;
- case FREE_TRIAL -> false;
- default -> throw new IllegalArgumentException("something is murky!");
- };
Multiline Strings (Preview)
您可以最后在Java中执行以下操作:
- String htmlBeforeJava13 = "<html>\n" +
- " <body>\n" +
- " <p>Hello, world</p>\n" +
- " </body>\n" +
- "</html>\n";
-
- String htmlWithJava13 = """
- <html>
- <body>
- <p>Hello, world</p>
- </body>
- </html>
- """;
Java 14 and later
一旦发布,将在此处进行介绍。 稍后检查。
Fin
到目前为止,您应该对以下几件事有了一个很好的概览:
How to install Java, which version to get and where to get it from (hint: AdoptOpenJDK).
What a Java distribution is, which ones exist and what the differences are.
What the differences between the specific Java versions are.
始终欢迎反馈,更正和随意输入! 只需在下面留下评论即可。
谢谢阅读。
Acknowledgements
Stephen Colebourne, who wrote a fantastic article on the different, available Java distributions.
There's more where that came from
I'll send you an update when I publish new guides. Absolutely no spam, ever. Unsubscribe anytime. Register for the newsletter here: https://bit.ly/2K0Ao4F .