Java 9 New Features

Please refer to this page

Modular System

Modules have a concept of dependencies, can export a public API and keep implementation details hidden/private.
Q1: How do we know the JAR includes all classes that we required?
Q2: How do we know the JAR whether includes reduplicate classes?

JLink

 

This tool allow you create a minimum RUNTIME image that don’t need load the all of JDK.

JShell

Let’s say goodbye to this redundant words: “public static void main(String[] args)”

It’s an interactive tool to evaluate declarations, statements, and expressions of Java, together with an API

New APIs

 

Immutable Set/List

In Java 8 creating a Set/List of several elements would require several lines of code. Now we can do it as simple as:

Set<Integer> numbers = Set.of(1, 2, 3);
List<String> strings = List.of("first", "second");

It is immutable – if we try to add or remove elements, an UnsupportedOperationException will be thrown

Optional to Stream

Stream add more 4 method: takeWhile, dropWhile, ofNullable, interate.

Stream stream = List.of(1,2,3,2,1).stream()
stream.takeWhile(i->i<3).forEach(System.out::print) // 1,2
stream.dropWhile(i->i<3).forEach(System.out::print) // 3,2,1

stream = obj == null ? Stream.emtpy(): Stream.of(obj);
stream = Stream.ofNullable(obj);

allow break the forEach:
IntStream.iterate(1, i -> i < 100, i -> i + 1)
.forEach(System.out::print);

Stream<Integer> s = Optional.of(1).stream()

Http/2 HttpClient and HttpRequest

It should support both HTTP/2 protocol and WebSocket handshake, with performance that should be comparable with the Apache HttpClient, Netty and Jetty

HttpClient client = HttpClient.newHttpClient();
HttpRequest req = HttpRequest.newBuilder(URI.create("http://www.test.com"))
              .header("User-Agent","Java").GET().build();
HttpResponse<String> resp = client.send(req, HttpResponse.BodyHandler.asString());

URI httpURI = new URI("http://www.test.com");
HttpRequest request = HttpRequest.create(httpURI).GET();
HttpResponse response = request.response();
String responseBody = response.body(HttpResponse.asString());

Process API

The current method returns an object representing a process of currently running JVM

ProcessHandle self = ProcessHandle.current();
long PID = self.getPid();
ProcessHandle.Info procInfo = self.info();
  
Optional<String[]> args = procInfo.arguments();
Optional<String> cmd =  procInfo.commandLine();
Optional<Instant> startTime = procInfo.startInstant();
Optional<Duration> cpuUsage = procInfo.totalCpuDuration();

ProcessHandle.current().children().forEach(procHandle.destroy());

Interface Private Method Try With Resources

As we know, Java 8 allow us add default method. But it’s public, can’t be private.
Now, Java 9 allow you add some private method!

public interface MyInterface {
    void normalInterfaceMethod();
    default void interfaceMethodWithDefault() {  init(); }
    default void anotherDefaultMethod() { init(); }
    private void init() { System.out.println("Initializing"); }
}

In Java 7, the try-with-resources syntax requires a fresh variable to be declared for each resource being managed by the statement.

try(InputStream input = new InputStream(...)) {
// do something
}

In Java 9 there is an additional refinement: if the resource is referenced by a final or effectively final variable, a try-with-resources statement can manage a resource without a new variable being declared:

InputStream input = new InputStream(...);
try(input) {
// do something
}

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Scroll to Top