Article with you to understand Java 11 new features

Most developers still immersed in the  Java 8 medium, but  Java 14 will be released on March 17th 2020, while I was still written on  Java 11 the new features. Java 11 Is the  Java 8 first LTS version later, but since the  Java 11 beginning, Oracle JDK no longer be free for commercial use, of course, if you are for personal use, or the use of Open JDK, it is still free to use.

Some people are concerned about  Java 11 whether the charges, Oracle representation unless you use in production, otherwise you can not charge.

Even charges, free Open JDK is not very fragrant it.

Free for production of Open JDK's official website:

And then six months later, Java 15 it would come, and this release is not only rhythm was a bit overwhelmed, but a bit confused. But anyway, the trend is not reversible, so a wave of tuition  Java 11 is necessary.

1.  String API

String is definitely one of the most commonly used Java class, the String class method of usage are also very high, in the  Java 11middle brings a series of useful operations for the String class.

  1. isBlank () sentenced empty.

    // 判空,blank里我放入了全角空格,半角空格,TABString blank = "    ";System.out.println(blank.isBlank());// 输出// true
    复制代码
  2. lines () gets the string split stream.

    // lines 返回一个 StreamString line = "a\nb\nc";Stream<String> lines = line.lines();// 使用 lambda 遍历lines.forEach(System.out::println);// 输出// a// b// c
    复制代码
  3. repeat () Copy String

    // 复制字符串String repeat = "我的微信:wn8398,";String repeat3 = repeat.repeat(3);System.out.println(repeat3);// 输出// 我的微信:wn8398,我的微信:wn8398,我的微信:wn8398,
    复制代码
  4. strip () before and after removal of whitespace characters.

    // 去除前后空白String strip = "   &emsp; https://www.wdbyte.com &emsp;";System.out.println("==" + strip.trim() + "==");// 去除前后空白字符,如全角空格,TABSystem.out.println("==" + strip.strip() + "==");// 去前面空白字符,如全角空格,TABSystem.out.println("==" + strip.stripLeading() + "==");// 去后面空白字符,如全角空格,TABSystem.out.println("==" + strip.stripTrailing() + "==");// 输出// ==&emsp; https://www.wdbyte.com &emsp;==// ==https://www.wdbyte.com==// ==https://www.wdbyte.com &emsp;==// ==   &emsp; https://www.wdbyte.com==
    复制代码

    Note here that trim only remove half-width space, and  strip is the removal of all kinds of whitespace .

2. File API

Read and write files easier.

// 创建临时文件Path path = Files.writeString(Files.createTempFile("test", ".txt"), "https://www.wdbyte.com");System.out.println(path);// 读取文件// String ss = Files.readString(Path.of("file.json"));String s = Files.readString(path);System.out.println(s);// 结果// https://www.wdbyte.com
复制代码

3. JEP 321 - HTTP Client

In  Java 11 the Http Client API has been standardized support. And supports HTTP / 1.1 and HTTP / 2, also supported websockets.

You can initiate a request like this.

HttpClient client = HttpClient.newHttpClient();HttpRequest request = HttpRequest.newBuilder()        .uri(URI.create("https://www.hao123.com"))        .build();// 异步client.sendAsync(request, HttpResponse.BodyHandlers.ofString())        .thenApply(HttpResponse::body)        .thenAccept(System.out::println)        .join();// 同步HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());System.out.println(response.body());
复制代码

More synchronous asynchronous request, concurrent access, proxy settings, etc., can refer to the OpenJDK official documents.

You also need to rely on a variety of HTTP Client bag?

4. JEP 323 - Lambda inferred local variables

In  Java 10 the introduction of the  var syntax, it can automatically infer variable types. In  Java 11 can be used in Lambda expressions in this syntactic sugar

var hashMap = new HashMap<String, Object>();hashMap.put("wechat", "wn8398");hashMap.put("website", "https://www.wdbyte.com");hashMap.forEach((var k, var v) -> {    System.out.println(k + ": " + v);});
复制代码

It should be noted that the (var k,var v) type, k and v are either using var, or do not write, or write the correct variable type. Var can not mix types and other variables.

image

It can not be mixed in the Lambda var

5. JEP 330 - the single instruction to run the Java

Since the first day of learning Java, we know to run a Java file, first with the  javac command to compile, and then  java the command to run, and now as long as a  java command to run.

$ cat Main.javapublic class Main {    public static void main(String[] args) {        System.out.println("wechat:wn8398");    }}$ java Main.javawechat:wn8398
复制代码

6. Free flight recorder

Commercial version of the JDK has been a low-cost event information collection tool, which is the flight recorder (Java Flight Recorder), which can be checked JVM, analyze, document and so on. When an unknown anomaly fault analysis can be carried out by recording. This easy to use tool  Java 11 for free in the open. Everyone can use this function.

Other updates

  1. JEP 309 - add dynamic file constant.
  2. JEP 318 - Add Epsilon garbage collector.
  3. JEP 320 - 删除 Java EE sum corba Mo块 (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).
  4. JEP 329 - increase the encryption algorithm chacha20, the realization poly1305.
  5. JEP 333 - the introduction of ZGC garbage collector experimental, shut down time to ensure that no more than 10ms.
  6. JEP 335 - abandoned Nashorn JavaScript engine

Guess you like

Origin juejin.im/post/5e5cb0616fb9a07cb74bdadf
Recommended