Difference between revisions of "Changes in Java 10 and 11"
(Added type inference.) |
(Added bits.) |
||
Line 13: | Line 13: | ||
== Type Inference for Local Variables (v10) == | == Type Inference for Local Variables (v10) == | ||
− | Among the biggest additions to Java 10 is its [https://openjdk.java.net/jeps/286 ability to figure out the type | + | 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: | 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)); | ||
− | Notice how | + | 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]]. |
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>: | 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>: | ||
Line 35: | Line 36: | ||
There are obviously some limitations for this new expression, such as: | 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 | + | * 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), | * <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://www.devcodenote.com/2015/04/variable-capture-in-java.html capture variables] (which need to be final, so they can not be inferred), |
Revision as of 19:10, 21 November 2018
Contents
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:
MockEventHandler eh = new MockEventHandler(); eh.setOnSomeEvent(event -> new EventProcessor().process(event));
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 thatnull
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