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 11
middle brings a series of useful operations for the String class.
-
isBlank () sentenced empty.
// 判空,blank里我放入了全角空格,半角空格,TABString blank = "   ";System.out.println(blank.isBlank());// 输出// true 复制代码
-
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 复制代码
-
repeat () Copy String
// 复制字符串String repeat = "我的微信:wn8398,";String repeat3 = repeat.repeat(3);System.out.println(repeat3);// 输出// 我的微信:wn8398,我的微信:wn8398,我的微信:wn8398, 复制代码
-
strip () before and after removal of whitespace characters.
// 去除前后空白String strip = "   https://www.wdbyte.com  ";System.out.println("==" + strip.trim() + "==");// 去除前后空白字符,如全角空格,TABSystem.out.println("==" + strip.strip() + "==");// 去前面空白字符,如全角空格,TABSystem.out.println("==" + strip.stripLeading() + "==");// 去后面空白字符,如全角空格,TABSystem.out.println("==" + strip.stripTrailing() + "==");// 输出// ==  https://www.wdbyte.com  ==// ==https://www.wdbyte.com==// ==https://www.wdbyte.com  ==// ==   https://www.wdbyte.com== 复制代码
Note here that
trim
only remove half-width space, andstrip
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.
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
- JEP 309 - add dynamic file constant.
- JEP 318 - Add Epsilon garbage collector.
- 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).
- JEP 329 - increase the encryption algorithm chacha20, the realization poly1305.
- JEP 333 - the introduction of ZGC garbage collector experimental, shut down time to ensure that no more than 10ms.
- JEP 335 - abandoned Nashorn JavaScript engine