Difference between revisions of "Changes in Java 10 and 11"

From p0f
Jump to: navigation, search
(Started writing.)
 
(Type Inference for Local Variables (v10): Added a method reference example. Fixed some formatting.)
(4 intermediate revisions by the same user not shown)
Line 8: Line 8:
  
 
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, [https://openjdk.java.net/projects/jdk/10/ Java 10] and [https://openjdk.java.net/projects/jdk/11/ Java 11].
 
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, [https://openjdk.java.net/projects/jdk/10/ Java 10] and [https://openjdk.java.net/projects/jdk/11/ Java 11].
 +
 +
== What I assume ==
 +
 +
I assume that you are:
 +
 +
* fluent in the Java language (according to v8 language specification)
 +
* aware of how Java garbage collectors work and why we need them
 +
* familiar with what software profiling and auditing are and how they work
 +
* have a clue about Linux scripting
  
 
= Language =
 
= Language =
Line 13: Line 22:
 
== Type Inference for Local Variables (v10) ==
 
== Type Inference for Local Variables (v10) ==
  
Among the biggest additions to Java 10 is its ability to "guess" the type of a variable that has an initialiser, that is to say, perform LHS (left-hand-side) type inference.
+
Among the biggest additions to Java 10 is its [https://openjdk.java.net/jeps/286 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 - [https://openjdk.java.net/projects/lambda/ 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:
 +
 
 +
MockEventHandler eh = new MockEventHandler();
 +
eh.setOnSomeEvent('''event -> new EventProcessor().process(event)''');
 +
 
 +
Actually, the above expression can be simplified even further:
 +
 
 +
MockEventHandler eh = new MockEventHandler();
 +
eh.setOnSomeEvent('''EventProcessor::process''');
 +
 
 +
Notice how parameters in lambda expressions lack type declarations. Infact, they completely lack ''any'' declarations! You can [[Java 8 Lambda Expressions|read more about this in a small write-up of mine]].
  
RHS type inference should be quite familiar by now as it was by far the biggest change in Java 8 - [https://openjdk.java.net/projects/lambda/ Lambda Expressions for the Java Language] allow us to save space, time, and improve readability, making heavy use of type inference all the time:
+
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'' <code>var</code>:
  
  example here
+
  '''var''' x = new Integer(5);
 +
'''var''' y = MyClass.Factory.getInstance();
 +
 +
if (x instanceof Integer) {
 +
    // matches
 +
}
 +
if (y instanceof MyClass) {
 +
    // matches
 +
}
  
Notice how parameter declarations in lambda lack type definitions. You can [[Java 8 Lambda Expressions|read more about this in a small write-up of mine]].
+
There are obviously some limitations for this new expression, such as:
  
In Java 10, this is extended even further. For cases when the compiler can infer the type of a variable, its declaration can be simplified to just the keyword <code>var</code>:
+
* declarations without initialisers (which would require some distant action to infer the type, and that could violate the strongly-typed nature of the language because of multiple possible ''different'' initialisers at different execution points),
 +
* <code>null</code> assignments (note that <code>null</code> ''is'' a type, but it is fairly useless in and of its own),
 +
* [http://www.devcodenote.com/2015/04/variable-capture-in-java.html capture variables] (which need to be final, so they can not be inferred),
 +
* [http://iteratrlearning.com/java/generics/2016/05/12/intersection-types-java-generics.html intersection types] (which can not be reliably inferred),
  
another example here
+
or generally whenever an assignment type is not denotable (such as with inline arrays, method references, and even <code>c.getClass()</code>).
  
Trololo.
+
I may write more about those limitations when I've used LHS type inference more, until then feel free to have a look at [https://openjdk.java.net/jeps/286 the JEP defining Java type inference] and its many references.
  
 
== Local Variable Syntax for Lambda Parameters (v11) ==
 
== Local Variable Syntax for Lambda Parameters (v11) ==

Revision as of 08:05, 26 November 2018

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.

What I assume

I assume that you are:

  • fluent in the Java language (according to v8 language specification)
  • aware of how Java garbage collectors work and why we need them
  • familiar with what software profiling and auditing are and how they work
  • have a clue about Linux scripting

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:

MockEventHandler eh = new MockEventHandler();
eh.setOnSomeEvent(event -> new EventProcessor().process(event));

Actually, the above expression can be simplified even further:

MockEventHandler eh = new MockEventHandler();
eh.setOnSomeEvent(EventProcessor::process);

Notice how parameters in lambda expressions lack type declarations. Infact, they completely lack any declarations! 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 strongly-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