There are only 30 days left before the end of 2019. Are you ready for 2020?

At the end of the year, people are particularly prone to recall and comparison, such as these days, the comparison challenge is hot!

The topic became a top trending trending topic on Weibo and flooded wechat moments, with people sharing photos of themselves comparing 2017 and 2019.

As a tech nerd, I also made a comparison:

On September 21, 2017, Java 9 was officially released, and in August 2017, the JCP Executive Committee proposed to change the release frequency of Java to every six months. The new release cycle will be strictly timed and will be released in March and September each year.

Comparison of previous JDK

Java 9 was officially released on September 22, 2017, with a number of new features, the main change being the modular system that has been implemented.

Main features:

  • Module system: Modules are containers for packages, and one of the biggest changes in Java 9 is the introduction of a module system (Project Jigsaw).
  • HTTP2 Client: The HTTP/2 standard is the latest version of the HTTP protocol. The new HTTPClient API supports WebSocket and HTTP2 streams as well as server push features.
  • Improved Javadoc: Javadoc now supports searching in API documents. In addition, Javadoc’s output is now compliant with the HTML5 standard.
  • Collection factory methods: In the List, Set, and Map interfaces, new static factory methods can create immutable instances of these collections.
  • Private interface methods: Use private interface methods. We can use private access modifiers to write private methods in our interfaces.
  • Improved Stream API: The improved Stream API adds convenience methods to make streaming easier, and uses collectors to write complex queries.
  • Improve the try – with – resources: If you already have a resource that is final or its equivalent, you can use that variable in the try-with-resources statement without declaring a new variable in the try-with-resources statement.
  • Improved Deprecated annotation @deprecated: The annotation @deprecated marks the status of a Java API, indicating that the marked API will be removed or has been broken.
  • Improvements to the Optional class: Java.util.Optional adds a number of new useful methods that can be turned directly into streams.
  • Reactive Streams API: A new Reactive Streams API has been introduced to support Reactive programming in Java 9.

Java 10, officially released on March 21, 2018, is the tenth major release of the Java language, one of the most widely used programming languages today.

Main features:

  • Local-variable Type Inference (LOCAL-variable Type Inference) : Using the var keyword to declare variables, it is possible to carry out the Inference of Variable types.
  • Parallel Full GC for G1: The G1 garbage collector’s Parallel Full garbage collection implements parallelism to improve worst-case latency.
  • Application Class-data Sharing: Application Class Data (AppCDS) Sharing reduces memory footprint and startup time by Sharing common Class metadata across processes.
  • ThreadLocal Handshakes: Callbacks to threads without running into a global JVM Safepoint. Optimizations can stop only a single thread, rather than all or none at all.

Java 11 was officially released on September 25, 2018 and is available in production environments! This is the first long-term support release since Java 8 and will be available until 2026.

Main features:

  • ZGC, extensible low-latency garbage collector (ZGC: A Scalable low-latency Garbage Collector) : ZGC is A Garbage Collector that claims to guarantee pauses of no more than 10MS per GC and A throughput decrease of no more than 15% compared to the current default G1 Garbage Collector.
  • Epsilon: A no-op Garbage Collector: This is A Garbage Collector that doesn’t do Garbage collection. This garbage collector is seemingly useless and can be used primarily for performance testing, memory stress testing, etc. The Epsilon GC can be used as a comparison to measure the performance of other garbage collectors.
  • In Java 11, var can be used as a Local Variable declaration of a Lambda expression.

Java 12 was officially released on March 19, 2019, another release update six months after the release of Java 11, the Long Term Support version

Main features:

  • Low pause time GC (Shenandoah: A low-pause-time Garbage Collector (Experimental) : Added Shenandoah algorithm to reduce GC Pause times by performing parallel evacuation work with running Java threads.
  • Switch Expressions: Extends the Switch statement so that it can be used as an expression as well as a statement
  • G1 Mixed GC (Abortable Mixed Collections for G1) similar to Return Unused Committed Memory from G1: Make the G1 Mixed GC abortive if there is a possibility that it will exceed the pause target.

Java 13 was released on September 17, 2019, introducing features such as text blocks.

Main features:

  • Extended Application Classes – Data Sharing (Dynamic CDS Archives) : to allow Dynamic archiving of classes at the end of Java application execution. The archive classes will include all loaded application classes and library classes that do not exist in the default base-tier CDS (Class Data-sharing) archive.
  • Enhance THE ZGC to return Unused heap Memory to the operating system (ZGC: Uncommit Unused Memory) : The ZGC can return Unused heap Memory to the operating system
  • Switch Expressions that can be used in a production environment: Yield statements are introduced in the switch block to return values.
  • Add Text Blocks to the Java language: Introduce multi-line string literals where you can place multi-line strings without any escape.

What features change the way you code?

1. Local variable type inference

In previous versions, we wanted to define when defining local variables. We need to provide an explicit type on the left side of the assignment and an implementation type on the right side of the assignment, as shown in the following fragment:

MyObject value = new MyObject();
List list = new ArrayList();
Copy the code

In Java 10, you can define objects like this:

var value = new MyObject();
var list = new ArrayList();
Copy the code

As you can see, local variable type inference will introduce the “var” keyword without explicitly specifying the type of the variable.

switch

Switch expressions were introduced as a preview feature in JDK 12. This feature was modified in Java 13 to introduce the yield statement, which is used to return values. This means that switch expressions (which return values) should use yield, and switch statements (which do not return values) should use break.

In the past, we wanted to return the content in switch, or rather cumbersome, the general syntax is as follows:

int i;
switch (x) {
    case "1":
        i=1;
        break;
    case "2":
        i=2;
        break;
    default:
        i = x.length();
        break;
}
Copy the code

Use the following syntax in JDK13:

int i = switch (x) { case "1" -> 1; case "2" -> 2; default -> { int len = args[1].length(); yield len; }};Copy the code

or

int i = switch (x) { case "1": yield 1; case "2": yield 2; default: { int len = args[1].length(); yield len; }};Copy the code

3. Text block support

A text block, a block of text, is a multi-line string literal that avoids the need for most escape sequences, automatically formats the string in a predictable way, and lets the developer control the formatting when needed.

We used to copy a text string from the outside into Java, which would be automatically escaped, such as the following string:

 <html>
  <body>
      <p>Hello, world</p>
  </body>
</html>
Copy the code

Copying it into a Java string displays the following:

"<html>\n" +
"    <body>\n" +
"        <p>Hello, world</p>\n" +
"    </body>\n" +
"</html>\n";
Copy the code

In JDK 13, you can use the following syntax:

"""
<html>
  <body>
      <p>Hello, world</p>
  </body>
</html>
""";
Copy the code

Using “” “as the beginning and end of a text block allows you to place a multi-line string in it without any need for escape. It looks very clean.

The latter

From 2017 to 2019, five versions of Java were released in just two years. There are a number of features coming with each release, and while none of them offer major changes like functional programming like Java 8, they do offer long-awaited features like switch expressions and Block text.

Two years later, have you made any progress in Java learning? Welcome to talk about your changes in the last two years.

About the author: Hollis, a person with a unique pursuit of Coding, is a technical expert of an Internet company, a personal technical blogger, who has read tens of millions of technical articles on the Internet, and co-author of three Courses for Programmers.