Changes in Java 10 and 11

From p0f
Revision as of 18:07, 21 November 2018 by Gregab (talk | contribs) (Added type inference.)
Jump to: navigation, search

Introduction

JDK 11 was released almost two months ago (as of this writing), yet it almost seems as if nobody noticed it.

After the roar and thunder of Java 9's JPMS (or Jigsaw, as some may know it) it's gone awfully quiet on the Java front.

But that's far from saying nothing is happening - after Java Module System (which was about the single biggest feature in Java 9), the world moves on.

If version 9 was mostly a clean-up release with the much-hated Java Plugin and the Applet API having been deprecated, CMS GC and all garbage collector combinations involving it, as well, several rather big improvements and additions had been made to both the language and the virtual machine in the two subsequent versions, Java 10 and Java 11.

Language

Type Inference for Local Variables (v10)

Among the biggest additions to Java 10 is its ability to figure out the type of a variable that has an initialiser, that is to say, perform LHS (left-hand-side) type inference.

RHS type inference should be quite familiar by now as it was by far the biggest change in Java 8 - Lambda Expressions for the Java Language allow a programmer to save space, time, and improve readability, making heavy use of type inference all the time:

example here

Notice how parameter declarations in lambda lack type definitions. You can read more about this in a small write-up of mine.

In Java 10, this inference is extended even further. For cases when the compiler can infer the type of a variable, its declaration can be simplified to just the newly introduced reserved type var:

var x = new Integer(5);
var y = MyClass.Factory.getInstance();

if (x instanceof Integer) {
    // matches
}
if (y instanceof MyClass) {
    // matches
}

There are obviously some limitations for this new expression, such as:

  • declarations without initialisers (which would require some distant action to infer the type, and that could violate the strong typed nature of the language because of multiple possible different initialisers at different execution points),
  • null assignments (note that null is a type, but it is fairly useless in and of its own),
  • capture variables (which need to be final, so they can not be inferred),
  • intersection types (which can not be reliably inferred),

or generally whenever an assignment type is not denotable (such as with inline arrays, method references, and even c.getClass()).

I may write more about those limitations when I've used LHS type inference more, until then feel free to have a look at the JEP defining Java type inference and its many references.

Local Variable Syntax for Lambda Parameters (v11)

BLABLA

New Nest-Based Field/Method Access Control (v11)

BLABLA



Virtual Machine

Application Class Data Sharing (v10)

BLABLA

No-Op Garbage Collector (v11)

BLABLA

ZGC (v11)

BLABLA


Tools

Flight Recorder (v11)

BLABLA

Low-Impact Heap Profiling (v11)

BLABLA

Launching Single-File Programs from Source (v11)

BLABLA