What functions have been optimized in JDK11 and what new features have been added | Feature analysis of each version of JDK

Insert image description here

I. Introduction

The last issue talked about some new features of JDK10. Friends who need to review it can go to this column to review it.

This issue talks about some new features of JDK11

2. New features

The following are some new or changed features in JDK 11:

1. Pure string type HTTP client:

JDK 11 introduces a new client API for the HTTP protocol, which provides a simple way to send HTTP requests and handle responses. Here's a simple example:

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class HttpClientExample {
    
    
    public static void main(String[] args) {
    
    
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://api.example.com/users"))
                .build();

        client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
                .thenApply(HttpResponse::body)
                .thenAccept(System.out::println)
                .join();
    }
}

2. Enhancement of local variable type inference:

JDK 11 supports the use of the keyword in lambda expressions varand enables type inference in more scenarios. For example:

import java.util.List;

public class VarExample {
    
    
    public static void main(String[] args) {
    
    
        List<String> names = List.of("Alice", "Bob", "Charlie");

        names.forEach((var name) -> {
    
    
            System.out.println(name);
        });
    }
}

3. Deprecated G1 garbage collector:

JDK 11 deprecated some flag options in the G1 garbage collector, and their use is no longer recommended.

4. New methods of String class:

JDK 11 Stringadds some new methods in the class, such as isBlank(), lines(), strip(), stripLeading(), stripTrailing()etc., for convenient processing of strings.

Of course, there are other changes and new features, which continue as follows:

5. JDK 11 allows the use of var in parameters of lambda expressions

Local-Variable Syntax for Lambda Parameters: JDK 11 allows use in parameters of lambda expressions varto simplify code. For example:

import java.util.function.Predicate;

public class LambdaExample {
    
    
    public static void main(String[] args) {
    
    
        Predicate<String> startsWithA = (var s) -> s.startsWith("A");

        System.out.println(startsWithA.test("Apple")); // true
        System.out.println(startsWithA.test("Banana")); // false
    }
}

6. Nested access control

Nest-Based Access Control: JDK 11 introduces a new access control method, nested access control. In nested classes, private members of external classes can be accessed. For example:

public class Outer {
    
    
    private static String message = "Hello";

    public static class Inner {
    
    
        public void printMessage() {
    
    
            System.out.println(message);
        }
    }

    public static void main(String[] args) {
    
    
        Inner inner = new Inner();
        inner.printMessage();
    }
}

7. New garbage collector Epsilon

Epsilon: A No-Op Garbage Collector: JDK 11 introduces a new garbage collector called Epsilon, which is actually a no-op garbage collector used for performance tuning and testing purposes.

8. Unicode 10:

Unicode 10: JDK 11 updates the Unicode version to support more characters and symbols.

9. HTTP Client (Standard):

HTTP Client (Standard): JDK 11 introduces a new standard HTTP client, replacing the obsolete one HttpURLConnection. The new HTTP client API provides a simpler and more flexible way to make HTTP requests and handle responses.

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class HttpClientExample {
    
    
    public static void main(String[] args) throws Exception {
    
    
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://api.example.com/data"))
                .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

10. Allows running a single source file directly without compiling it first

Launch Single-File Source Code: JDK 11 allows running individual source files directly without compiling them first, which is very convenient for simple scripts and small applications.

public class HelloWorld {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("Hello, World!");
    }
}

Can be run directly:java HelloWorld.java

11. Introduced ZGC garbage collector

ZGC: A Scalable Low-Latency Garbage Collector: JDK 11 introduces ZGC (Z Garbage Collector), which is a low-latency garbage collector suitable for large memory applications and scenarios that require fast response.

12.JDK 11 deprecates the Security Manager and Java EE modules

Deprecate the Security Manager and the Java EE Modules: JDK 11 deprecates the Security Manager and Java EE modules, encouraging developers to use more modern security frameworks and technologies.

The above are some of the important features and improvements in JDK 11. This version also contains many other small improvements and fixes.

3. Remove and discard items

In JDK 11, some features have been removed or deprecated. Here are some detailed instructions and sample code:

  1. Removed features
  • Java EEModules: JDK 11 has removed some Java EE related modules, such as java.xml.ws, java.xml.bind, java.xml.ws.annotation, java.corbaetc. These modules have been deprecated in JDK 9. If you need to use these features, you may consider using Jakarta EE or other alternatives.

Sample code: In JDK 11, if the following import statement is used in your code, a compilation error will occur:

import javax.xml.ws.WebServiceClient;
import javax.xml.bind.annotation.XmlRootElement;
  1. Deprecated functionality
  • Security Manager: In JDK 11, Security Manager is deprecated. Security Manager is a security mechanism used to control access rights to Java applications. In JDK 11, developers are encouraged to use more modern security frameworks and technologies, such as a combination of Security Manager and Security Policy files.

Sample code: In JDK 11, if your code uses Security Manager-related APIs, such as the following code, a warning will appear during compilation:

System.setSecurityManager(new SecurityManager());
  • Pack200Tools and API: Pack200 is a tool for compressing and decompressing JAR files. In JDK 11, the Pack200 tools and related APIs are deprecated. This is because the Pack200 tool is not commonly used in practice and there are better alternatives.

Sample code: In JDK 11, if your code uses Pack200 related classes and methods, such as the following code, a warning will appear during compilation:

import java.util.jar.Pack200;

public class Example {
    
    
    public static void main(String[] args) {
    
    
        Pack200.Unpacker unpacker = Pack200.newUnpacker();
        // ...
    }
}
  • Other deprecated methods and classes: JDK 11 also deprecated some long-deprecated methods and classes, such as Runtime.runFinalizersOnExit()and Thread.stop(Throwable). These methods and classes are deprecated and may be removed in future JDK versions.

Sample code: In JDK 11, if your code uses deprecated methods and classes, such as the following code, a warning will appear during compilation:

Runtime.runFinalizersOnExit(true);
Thread.stop(new Throwable());

Please note that the above sample code is only used to illustrate the features removed and deprecated in JDK 11, and may not be applicable to all scenarios. In actual use, please refer to the official documentation of JDK 11 and more specific sample code.

おすすめ

転載: blog.csdn.net/qq_27471405/article/details/133402513