
Java 11 is another LTS version since Java 8 and is currently one of the most used LTS versions in the world. Today we continueJava 9 to Java 17In this series of articles, let's learn about Java 11 for ordinary developers.
String API enhancements
In Java 11,String
The operation has been further strengthened. Avoid us introducing extra, complex APIs in very common scenarios.
isBlank()
Used to determine whether a string is an empty character""
ortrim()
After that (" "
) is an empty character:
String blankStr = " ";
// true
boolean trueVal = blankStr.isBlank();
lines()
Sets a string according to the line terminator (newline feed\n
or carriage return\r
) divide and divide intoStream
Stream:
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
strip()
Remove the "full and half-angle" 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());
I couldn't help but think about ittrim()
Method, the difference can also be seen from the above.trim()
can only removehalf-angle
Blank mark.
strip()
There are two variants of the method,stripLeading()
Used to remove the front full-angle half-angle white space;stripTrailing()
Used to remove the full-angle half-angle white space at the tail.
repeat(n)
Repeat the contents of the concatenated string the given number of times:
String str = "HELLO";
//Empty character
String empty = str.repeat(0);
// HELLO
String repeatOne = str.repeat(1);
// HELLOHELLO
String repeatTwo = str.repeat(2);
Collection to array of corresponding type
Previously, it was very troublesome to convert a collection to a corresponding array, either use iteration; or useStream
Stream, now you can do this:
List<String> sampleList = Arrays.asList("felord.cn", "java 11");
// array = {"felord.cn", "java 11"};
String[] array = sampleList.toArray(String[]::new);
assertion negation
java.util.function.Predicate<T>
Is our very commonly used assertion predicate function. In the past, we had to use the help of!
Symbol, in Java 11 we can use its static methodsnot
to implement it, so that the semantics are 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 at the same time
.filter(Predicate.not(s -> s.contains("11")))
.collect(Collectors.toList());
in factPredicate<T>
In the original version, a default method for negation was also provided:
default Predicate<T> negate() {
return (t) -> ! test(t);
}
I have also used it for combination verification in previous articles. The scenarios of these two methods are different.
var can be used to modify Lambda local variables
Introduced in Java 10var
to make 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 the input parameters can actually be inferred based on the context. Take the example above,s -> s.startsWith("j")
ofs
Must be a string type, so in Java 11var
Can be used to modify Lambda local variables:
List<String> result = sampleList.stream()
//Filter strings starting with j
.filter((@NotNull var s) -> s.startsWith("j"))
//A string that does not contain 11 at the same time
.filter(Predicate.not((@NotNull var s) -> s.contains("11")))
.collect(Collectors.toList());
If we don't declare
var
There is no way to add@NotNull
Notes.
It is easier to read and write string content in files
In Java 11, it is easier to read and write strings from files, and we can use theFiles
New static methods provided by tool classesreadString
andwriteString
Reading and writing the string content of a file separately is a problem before, especially for students who are unfamiliar with IO streams. Now it takes a few simple lines:
String dir= "C://yourDir";
//Write file
Path path = Files.writeString(Files.createTempFile(dir, "hello", ".txt"), "hello java 11");
//Read files
String fileContent = Files.readString(path);
Access control rules for nested classes
Prior to Java 11, it was possible for inner nested classes to access private properties and methods of outer classes:
public class Outer {
private int outerInt;
class Inner {
public void printOuterField() {
System.out.println("Outer field = " + outerInt);
}
}
}
But if you use the reflection API to enable an inner class to access the private properties and methods of an outer class, it will throwIllegalStateException
abnormal. Java 11 fixes an issue where reflections cannot be accessed.
JVM access rules do not allow private access between nested classes。We can access it in a regular way because the JVM implicitly creates for us at compile timebridging method。Two new attributes were introduced in Java 11: one is called
NestMembers
attribute of, which identifies other known static nest members; the other is the attribute contained in each nest memberNestHost
Property that identifies its nest host class. The boarding relationship between the two parties is mapped during compilation time, and bridging is no longer needed.
HttpClient supports HTTP2
HttpClient
After Java 11, it began to support HTTP2, the underlying layer has been greatly optimized, and now fully supports asynchronous non-blocking.
HttpClient
The package name isjdk.incubator.http
changed tojava.net.http
。
other
In Java 11, there are also some other features and optimizations, such as the introduction of ZGC, support for the TLS 1.3 protocol, the introduction of an invokedynamic mechanism, and the original commercial version of JFR also carries out open source integration. Java Ecology Survey data at the beginning of the year showed that the number of users of Java 11 has increased significantly and has become one of the mainstream version choices.
Comments0