Java 11 is another LTS version since Java 8. It is one of the most used LTS versions in the world. Today, we will continue to learn about Java 11 for ordinary developers in the Java 9 to Java 17 series.
String API enhancements
In Java 11, the operation on String is further strengthened. Avoid introducing additional, complex API s in very common scenarios.
isBlank()
It is used to judge whether the string is a null character "" or a null character ("") after trim():
String blankStr = " "; // true boolean trueVal = blankStr.isBlank(); Copy code
lines()
Split a string according to the line terminator (line break \ n or carriage return \ r) and divide it into Stream streams:
String newStr = "Hello Java 11 \n felord.cn \r 2021-09-28"; Stream<String> lines = newStr.lines(); lines.forEach(System.out::println); // Hello Java 11 // felord.cn // 2021-09-28 Copy code
strip()
Remove "full and half width" white space characters before and after the string:
String str = "HELLO\u3000"; // str = 6 System.out.println("str = " + str.length()); // trim = 6 System.out.println("trim = " + str.trim().length()); // strip = 5 System.out.println("strip = " + str.strip().length()); Copy code
I can't help thinking about the difference between the trim() method and the above. trim() can only remove half width white space characters.
There are two variants of the strip() method. stripLeading() is used to remove the preceding full width and half width whitespace; stripTrailing() is used to remove the trailing full width and half width whitespace.
repeat(n)
Repeat the contents of the concatenated string a given number of times:
String str = "HELLO"; // Null character String empty = str.repeat(0); // HELLO String repeatOne = str.repeat(1); // HELLOHELLO String repeatTwo = str.repeat(2); Copy code
Set to array of corresponding type
Before, it was troublesome to convert the set to the corresponding array, or use iteration; Or use Stream. Now you can:
List<String> sampleList = Arrays.asList("felord.cn", "java 11"); // array = {"felord.cn", "java 11"}; String[] array = sampleList.toArray(String[]::new); Copy code
Assertion negation
Java. Util. Function. Predicate < T > is a very common assertion predicate function. In the past, we had to rely on! In Java 11, we can implement the symbol with the help of its static method not, which makes the semantics clearer:
List<String> sampleList = Arrays.asList("felord.cn", "java 11","jack"); // [jack] List<String> result = sampleList.stream() // Filter strings starting with j .filter(s -> s.startsWith("j")) // A string that does not contain 11 .filter(Predicate.not(s -> s.contains("11"))) .collect(Collectors.toList()); Copy code
In fact, predict < T > also provides a default method of negation in the initial version:
default Predicate<T> negate() { return (t) -> !test(t); } Copy code
I also used it for combination verification in previous articles. The scenarios of the two methods are different.
var can be used to modify Lambda local variables
Var introduced in Java 10 for type inference. In Java 10, it cannot be used to modify the input parameters of a Lambda expression. In fact, for a Lambda expression, the type of its input parameters can be inferred from the context. Take the above example, s in S - > s.startswith ("J") must be of string type, so var can be used to modify Lambda local variables in Java 11:
List<String> result = sampleList.stream() // Filter strings starting with j .filter((@NotNull var s) -> s.startsWith("j")) // A string that does not contain 11 .filter(Predicate.not((@NotNull var s) -> s.contains("11"))) .collect(Collectors.toList()); Copy code
If we do not declare var, there is no way to add @ NotNull annotation to the input parameter.
It is more convenient to read and write string content in the file
Java 11 makes it easier to read and write string contents from Files. We can read and write the string contents of Files through the new static methods readString and writeString provided by the Files tool class. It's a lot of trouble before, especially for students unfamiliar with IO streams. java training Now it's done in a few lines:
String dir= "C://yourDir"; // write file Path path = Files.writeString(Files.createTempFile(dir, "hello", ".txt"), "hello java 11"); // read file String fileContent = Files.readString(path); Copy code
Access control rules for nested classes
Before Java 11, it was feasible for internal nested classes to access private properties and methods of external classes:
public class Outer { private int outerInt; class Inner { public void printOuterField() { System.out.println("Outer field = " + outerInt); } } } Copy code
However, if you implement the internal class to access the private properties and methods of the external class through the reflection API, an IllegalStateException will be thrown. Java 11 fixed reflection inaccessibility
JVM access rules do not allow private access between nested classes. We can access it in the normal way because the JVM implicitly creates a bridging method for us at compile time. Java 11 introduces two new attributes: one is called nest members, which is used to identify other known static nest members; The other is the NestHost attribute contained in each nest member, which is used to identify its nest host class. The host relationship between the two sides is mapped at compile time, and there is no need for bridging.
HttpClient supports HTTP2
After HttpClient arrived at Java 11, it began to support HTTP2. The underlying layer has been greatly optimized, and now it fully supports asynchronous non blocking.
The package name of HttpClient is changed from jdk.incubator.http to java.net.http.
other
In Java 11, there are some other features and optimizations, such as the introduction of ZGC, support for TLS 1.3 protocol, the introduction of invokedynamic mechanism, and the open source integration of the original commercial version of JFR. The Java ecology survey data at the beginning of the year showed that the number of users of Java 11 increased significantly and became one of the mainstream versions.
Author: Ma Nong, little fat brother
Link: https://juejin.cn/post/7023219454095917086
Source: rare earth Nuggets
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.