Java 11功能

We haven’t fully immersed ourselves in Java 10 yet, and Java 11 is here. Java 11 is important for more than just a few reasons. Oracle has revamped its support model and come up with a release train that’ll bring rapid updates, about every 6 months.

我们还没有完全沉浸在Java 10中,而Java 11就在这里。 Java 11的重要意义不仅仅出于几个原因。 Oracle已对其支持模型进行了改进,并提出了大约每6个月进行一次快速更新的发行培训。

They’ve changed the licensing and support model which means if you download the Java 11 Oracle JDK, it will be paid for commercial use.

他们更改了许可和支持模型,这意味着,如果您下载Java 11 Oracle JDK,则将以商业用途付费。



NO. Not necessarily unless you download the Oracle JDK and use it in production. 没有。 除非下载Oracle JDK并在生产中使用它,否则不一定。

Note: IntelliJ IDEA 2018.2.4 Community Edition already has support for Java 11.

注意 :IntelliJ IDEA 2018.2.4 Community Edition已经支持Java 11。

1. Java 11为什么重要? (1. Why is Java 11 important?)

Java 11 is the second LTS release after Java 8. Since Java 11, Oracle JDK would no longer be free for commercial use.

Java 11是继Java 8之后的第二个LTS版本。自Java 11起,Oracle JDK将不再免费提供商业用途。

You can use it in developing stages but to use it commercially, you need to buy a license. If you don’t, you can get an invoice bill from Oracle any day!

您可以在开发阶段使用它,但要在商业上使用它,则需要购买许可证。 如果您不这样做,则可以随时从Oracle获得发票账单!

Java 10 was the last free Oracle JDK that could be downloaded.

Java 10是最后一个可以下载的免费Oracle JDK。

Oracle stops Java 8 support from January 2019. You’ll need to pay for more support.
You can continue using it, but won’t get any patches/security updates.

Oracle从2019年1月起停止对Java 8的支持。您需要付费才能获得更多支持。
您可以继续使用它,但不会获得任何补丁/安全更新。

Oracle will not be providing free long-term support (LTS) for any single Java version since Java 11.
自Java 11起,Oracle将不再为任何单个Java版本提供免费的长期支持(LTS)。

While Oracle JDK is no longer free, you can always download the Open JDK builds from Oracle or other providers such as AdoptOpenJDK, Azul, IBM, Red Hat, etc. In my opinion, unless you are looking for Enterprise level usage with the appetite to pay for the support fees, you can use OpenJDK and upgrade them as and when necessary.

尽管Oracle JDK不再免费,但是您始终可以从Oracle或其他提供商(例如AdoptOpenJDK,Azul,IBM,Red Hat等)下载Open JDK构建。在我看来,除非您对企业级的需求感兴趣。支付支持费用,您可以使用OpenJDK并在必要时对其进行升级。

2.我应该下载哪个JDK版本,每个版本都有什么好处? (2. Which JDK build should I download and what are the benefits of each of them?)

Since Oracle has created a release train in which a new version would come up every six months, if you are using the free Open JDK by Oracle, you will need to update it every six months, since Oracle won’t provide free updates once the new version is released. This can be challenging to adapt to a company.

由于Oracle已创建了一个发布培训,其中每六个月会发布一个新版本,因此,如果您使用Oracle提供的免费Open JDK,则您需要每六个月进行更新,因为一旦Oracle不再提供免费更新,则必须每六个月进行一次更新。新版本发布。 要适应公司,这可能是一个挑战。

Pay for commercial support to Oracle and migrate only from one LTS version to the next LTS version.
This way you’ll get all the updates and support for Java 11 till 2026. You can download Java 17 in 2022.

支付对Oracle的商业支持,并且只能从一个LTS版本迁移到下一LTS版本。
这样,您将获得Java 11的所有更新和支持,直到2026年。您可以在2022年下载Java 17。

Stay on free Java version even after its support ends. Though you won’t get security updates and it can open up security loopholes.

即使支持结束,也请继续使用免费的Java版本。 尽管您不会获得安全更新,但它可能会打开安全漏洞。

Oracle won’t provide commercial support or updates for Java 9 and Java 10. You need to look for other alternative builds in order to keep using them for free.
Oracle不会为Java 9和Java 10提供商业支持或更新。您需要寻找其他替代版本,以便继续免费使用它们。

Having understood the baggage Java 11 comes with, lets now analyze the important features in Java 11 for developers. We’ll discuss some important JEPs too.

了解了Java 11带来的负担后,现在让我们为开发人员分析Java 11中的重要功能。 我们还将讨论一些重要的JEP。

Note: JavaFX will be available as a separate module and not tied to Java JDK’s 6-month release cycle schedule.

注意:JavaFX将作为单独的模块提供,并且与Java JDK的6个月发布周期时间表无关。

3.如何下载Java 11免费版? (3. How to download Java 11 Free Version?)

You can download production ready OpenJDK version from this link. The binaries are in tar or zip format, so just unzip them and set the environment variables to use java compiler and java commands.

您可以从此链接下载可用于生产的OpenJDK版本。 二进制文件为tar或zip格式,因此只需解压缩它们并设置环境变量即可使用java编译器和java命令。

4. Java 11功能 (4. Java 11 Features)

Some of the important Java 11 features are:

Java 11的一些重要功能包括:

  • Running Java File with single command

    使用单个命令运行Java文件
  • New utility methods in String class

    String类中的新实用程序方法
  • Local-Variable Syntax for Lambda Parameters

    Lambda参数的局部变量语法
  • Nested Based Access Control

    基于嵌套的访问控制
  • JEP 321: HTTP Client

    JEP 321:HTTP客户端
  • Reading/Writing Strings to and from the Files

    在文件中读取/写入字符串
  • JEP 328: Flight Recorder

    JEP 328:飞行记录器

Let’s discuss the new features introduced with Java 11 from the JEP Process.

让我们讨论一下JEP Process随Java 11引入的新功能。

4.1)使用单个命令运行Java文件 (4.1) Running Java File with single command)

One major change is that you don’t need to compile the java source file with javac tool first. You can directly run the file with java command and it implicitly compiles.
This feature comes under JEP 330.

一个主要的变化是您不需要首先使用javac工具编译Java源文件。 您可以使用java命令直接运行该文件,并且该文件会隐式编译。
此功能属于JEP 330。

Following is a sneak peek at the new methods of Java String class introduced in Java 11:

以下是Java 11中引入的Java String类的新方法的先睹为快:

4.2)Java字符串方法 (4.2) Java String Methods)

isBlank() – This instance method returns a boolean value. Empty Strings and Strings with only white spaces are treated as blank.

isBlank() –此实例方法返回一个布尔值。 空字符串和仅包含空格的字符串将被视为空白。

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        
        System.out.println(" ".isBlank()); //true
        
        String s = "Anupam";
        System.out.println(s.isBlank()); //false
        String s1 = "";
        System.out.println(s1.isBlank()); //true
    }
}

lines()

lines()

This method returns a stream of strings, which is a collection of all substrings split by lines.

此方法返回字符串流,该字符串流是按行分割的所有子字符串的集合。

import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) throws Exception {
        
        String str = "JD\nJD\nJD"; 
        System.out.println(str);
        System.out.println(str.lines().collect(Collectors.toList()));
    }
}

The output of the above code is:

上面代码的输出是:

strip(), stripLeading(), stripTrailing()

strip(),stripLeading(),stripTrailing()

strip() – Removes the white space from both, beginning and the end of string.

strip() –删除字符串开头和结尾的空格。

But we already have trim(). Then what’s the need of strip()?

strip() is “Unicode-aware” evolution of trim().

When trim() was introduced, Unicode wasn’t evolved. Now, the new strip() removes all kinds of whitespaces leading and trailing(check the method Character.isWhitespace(c) to know if a unicode is whitespace or not)

但是我们已经有了trim()。 那么strip()的需求是什么?

strip()trim() “ Unicode感知”演变。

引入trim()时,Unicode尚未发展。 现在,新的strip()删除了所有前导和尾随的空格(检查Character.isWhitespace(c)方法以了解Unicode是否为空格)

An example using the above three methods is given below:

下面是使用上述三种方法的示例:

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        
        String str = " JD "; 
        System.out.print("Start");
        System.out.print(str.strip());
        System.out.println("End");
        
        System.out.print("Start");
        System.out.print(str.stripLeading());
        System.out.println("End");
        
        System.out.print("Start");
        System.out.print(str.stripTrailing());
        System.out.println("End");
    }
}

The output in the console from the above code is:

上面代码在控制台中的输出为:

repeat(int)

重复(int)

The repeat method simply repeats the string that many numbers of times as mentioned in the method in the form of an int.

repeat方法简单地以int形式将字符串重复多次(如方法中所述)。

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        
        String str = "=".repeat(2);
        System.out.println(str); //prints ==
    }
}

4.3)Lambda参数的局部变量语法 (4.3) Local-Variable Syntax for Lambda Parameters)

JEP 323, Local-Variable Syntax for Lambda Parameters is the only language feature release in Java 11.
In Java 10, Local Variable Type Inference was introduced. Thus we could infer the type of the variable from the RHS – var list = new ArrayList<String>();

JEP 323Lambda参数的本地变量语法是Java 11中唯一的语言功能版本。
在Java 10中,引入了局部变量类型推断 。 因此,我们可以从RHS推断变量的类型– var list = new ArrayList<String>();

JEP 323 allows var to be used to declare the formal parameters of an implicitly typed lambda expression.

JEP 323允许使用var声明隐式类型的lambda表达式的形式参数。

We can now define :

我们现在可以定义:

(var s1, var s2) -> s1 + s2

This was possible in Java 8 too but got removed in Java 10. Now it’s back in Java 11 to keep things uniform.

这在Java 8中也是可能的,但在Java 10中已被删除。现在,它又回到Java 11中来保持一致。

But why is this needed when we can just skip the type in the lambda?
If you need to apply an annotation just as @Nullable, you cannot do that without defining the type.

但是,当我们只需跳过lambda中的类型时,为什么需要这样做呢?
如果您需要像@Nullable一样应用注释,则不能在不定义类型的情况下这样做。

Limitation of this feature – You must specify the type var on all parameters or none.
Things like the following are not possible:

此功能的局限性 –您必须在所有参数上指定var类型,或者不指定任何类型。
不可能出现以下情况:

(var s1, s2) -> s1 + s2 //no skipping allowed
(var s1, String y) -> s1 + y //no mixing allowed

var s1 -> s1 //not allowed. Need parentheses if you use var in lambda.

4.4)基于嵌套的访问控制 (4.4) Nested Based Access Control)

Before Java 11 this was possible:

在Java 11之前,这是可能的:

public class Main {
 
    public void myPublic() {
    }
 
    private void myPrivate() {
    }
 
    class Nested {
 
        public void nestedPublic() {
            myPrivate();
        }
    }
}

private method of the main class is accessible from the above-nested class in the above manner.
But if we use Java Reflection, it will give an IllegalStateException.

可以通过上述方式从上述嵌套的类访问主类的private方法。
但是,如果我们使用Java Reflection ,它将给出一个IllegalStateException

Method method = ob.getClass().getDeclaredMethod("myPrivate");
method.invoke(ob);

Java 11 nested access control addresses this concern in reflection.
java.lang.Class introduces three methods in the reflection API: getNestHost(), getNestMembers(), and isNestmateOf().

Java 11嵌套访问控制解决了这一问题。
java.lang.Class在反射API中引入了三种方法: getNestHost()getNestMembers()isNestmateOf()

4.5)JEP 309:动态类文件常量 (4.5) JEP 309: Dynamic Class-File Constants)

The Java class-file format now extends support a new constant pool form, CONSTANT_Dynamic. The goal of this JEP is to reduce the cost and disruption of developing new forms of materializable class-file constraints, by creating a single new constant-pool form that can be parameterized with user-provided behavior.
This enhances performance

Java类文件格式现在扩展了对新常量池形式CONSTANT_Dynamic的支持。 该JEP的目标是通过创建可以用用户提供的行为进行参数化的单个新的常量池形式,来减少开发可实现的类文件约束的新形式的成本并减少对它的破坏。
这样可以提高性能

4.6)JEP 318:Epsilon:无操作垃圾收集器 (4.6) JEP 318: Epsilon: A No-Op Garbage Collector)

Unlike the JVM GC which is responsible for allocating memory and releasing it, Epsilon only allocates memory.
It allocates memory for the following things:

与负责分配内存和释放内存的JVM GC不同,Epsilon仅分配内存。
它为以下内容分配内存:

  • Performance testing.

    性能测试。
  • Memory pressure testing.

    内存压力测试。
  • VM interface testing.

    VM接口测试。
  • Extremely short lived jobs.

    寿命极短的工作。
  • Last-drop latency improvements.

    最后一滴延迟改进。
  • Last-drop throughput improvements.

    最终吞吐量提高。

Now Elipson is good only for test environments. It will lead to OutOfMemoryError in production and crash the applications.

现在,Elipson仅适用于测试环境。 这将导致生产中的OutOfMemoryError并使应用程序崩溃。

The benefit of Elipson is no memory clearance overhead. Hence it’ll give an accurate test result of performance and we can no longer GC for stopping it.

Elipson的好处是没有内存清除开销。 因此,它将给出准确的性能测试结果,我们不再可以通过GC来停止它。

Note: This is an experimental feature.

注意:这是一项实验功能。

4.7)JEP 320:删除Java EE和CORBA模块 (4.7) JEP 320: Remove the Java EE and CORBA Modules)

The modules were already deprecated in Java 9. They are now completely removed.

Java 9中已经弃用了这些模块,现在将它们完全删除。

Following packages are removed: 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.xml.wsjava.xml.bindjava.activationjava.xml.ws.annotationjava.corbajava.transactionjava.se.eejdk.xml.wsjdk.xml.bind

4.8)JEP 328:飞行记录器 (4.8) JEP 328: Flight Recorder)

Flight Recorder which earlier used to be a commercial add-on in Oracle JDK is now open-sourced since Oracle JDK is itself not free anymore.

Flight Recorder以前是Oracle JDK中的商业附加组件,现已开放源代码,因为Oracle JDK本身已不再免费。

JFR is a profiling tool used to gather diagnostics and profiling data from a running Java application.
Its performance overhead is negligible and that’s usually below 1%. Hence it can be used in production applications.

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

4.9)JEP 321:HTTP客户端 (4.9) JEP 321: HTTP Client)

Java 11 standardizes the Http CLient API.
The new API supports both HTTP/1.1 and HTTP/2. It is designed to improve the overall performance of sending requests by a client and receiving responses from the server. It also natively supports WebSockets.

Java 11标准化了Http CLient API。
新的API支持HTTP / 1.1和HTTP / 2。 它旨在提高客户端发送请求和从服务器接收响应的整体性能。 它还原生支持WebSockets。

4.10)在文件中读取/写入字符串 (4.10) Reading/Writing Strings to and from the Files)

Java 11 strives to make reading and writing of String convenient.
It has introduced the following methods for reading and writing to/from the files:

Java 11致力于使String的读取和写入变得方便。
它引入了以下用于读写文件的方法:

  • readString()

    readString()
  • writeString()

    writeString()

Following code showcases an example of this

以下代码展示了此示例

Path path = Files.writeString(Files.createTempFile("test", ".txt"), "This was posted on JD");
System.out.println(path);
String s = Files.readString(path);
System.out.println(s); //This was posted on JD

4.11)JEP 329:ChaCha20和Poly1305加密算法 (4.11) JEP 329: ChaCha20 and Poly1305 Cryptographic Algorithms)

Java 11 provides ChaCha20 and ChaCha20-Poly1305 cipher implementations. These algorithms will be implemented in the SunJCE provider.

Java 11提供了ChaCha20和ChaCha20-Poly1305密码实现。 这些算法将在SunJCE提供程序中实现。

4.12)JEP 315:改进Aarch64本征 (4.12) JEP 315: Improve Aarch64 Intrinsics)

Improve the existing string and array intrinsics, and implement new intrinsics for the java.lang.Math sin, cos, and log functions, on AArch64 processors.

改进现有的字符串和数组内在函数,并在AArch64处理器上为java.lang.Math sin,cos和log函数实现新的内在函数。

4.13)JEP 333:ZGC:可扩展的低延迟垃圾收集器(实验性) (4.13) JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental))

Java 11 has introduced a low latency GC. This is an experimental feature.
It’s good to see that Oracle is giving importance to GC’s.

Java 11引入了低延迟GC。 这是一项实验功能。
很高兴看到Oracle在重视GC。

4.14)JEP 335:弃用Nashorn JavaScript引擎 (4.14) JEP 335: Deprecate the Nashorn JavaScript Engine)

Nashorn JavaScript script engine and APIs are deprecated thereby indicating that they will be removed in the subsequent releases.

Nashorn JavaScript脚本引擎和API已过时,表示它们将在后续版本中删除。

5.结论 (5. Conclusion)

We’ve gone through the important features and updates provided in Java 11. See you soon when Java 12 releases.

我们已经完成了Java 11中提供的重要功能和更新。Java 12发行时将再见。

翻译自: https://www.journaldev.com/24601/java-11-features

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值