The source code is for testing only and should not be used commercially. The source code comes from the Internet. If there is any infringement, please contact me to remove it.
从Java 9 到 Java 17之Java 11

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,StringThe 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\nor carriage return\r) divide and divide intoStreamStream:

        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-angleBlank 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 useStreamStream, 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 methodsnotto 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 10varto 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")ofsMust be a string type, so in Java 11varCan 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 declarevarThere is no way to add@NotNullNotes.

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 theFilesNew static methods provided by tool classesreadStringandwriteStringReading 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 throwIllegalStateExceptionabnormal. 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 member NestHost 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

HttpClientAfter 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 is jdk.incubator.http changed to java.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.

read more
Resource download
PriceFree
The use is limited to testing, experiments, and research purposes. It is prohibited for all commercial operations. This team is not responsible for any illegal behavior of users during use. Please self-test all source codes! There is no guarantee of the integrity and validity of your source code. All source code is collected from the entire network
Original link:https://bcbccb.cn/en/3131.html, please indicate the source for reprinting. Disclaimer: This resource has not been authorized by the original rights holder and is not commercially available. It can only be used to learn and analyze the underlying code, CSS, etc., and is prohibited for commercial purposes. Any relevant disputes and legal liabilities arising from unauthorized commercial use shall be fully borne by the user. Everyone is responsible to support genuine copies. Please delete them within 24 hours after downloading. Thank you for your support!
1

Comments0

完整版APP区块链游戏源码,trx投注竞猜游戏,trx下注游戏,前端uinapp
Full version of APP blockchain game source code, trx betting game, trx betting game, front-end uinapp
Someone bought it 5 minutes ago Go and have a look

Site Announcements

The source code (theme/plug-in/application source code) and other resources provided by this site are only for learning and exchange

Commercial use is prohibited, otherwise all consequences will be borne by the downloading user!

Some resources are collected or copied online. If they infringe on your legitimate rights and interests, please write to us.

Currently, members have a big reward, and the current price for a lifetime member is 299 gold coins.Recent price adjustments

Join quickly, opportunities wait for no one! immediately participated in

Captcha

Fast login to social accounts

en_USEnglish