Oracle has released a new version of Java - Java 10. This release comes just six months after the release of Java 9 and like the previous release, it is another feature release with a lifespan of only six months.
The release of Java 10 immediately obsoletes Java 9 and no further free support releases of Java 9 will be made available. As of March 20th, the only versions of Java with free support from Oracle are Java 8 and Java 10. The next release with long-term support available is Java 11, which will be released in September 2018.
The final release is virtually unchanged from the RC1 candidate and no significant features have been added since InfoQ first reported on the release contents in November 2017.
Full documentation of this release is available as well as release notes for the language-level changes in this version.
For the developer, one major language feature is the arrival of var
. This is an enhancement to Java's type inference capabilities that may prove to be more significant than it first appears. In the simplest case var
allows code such as:
var foo = new ArrayList<String>();
which moves the inference from the type of values to the variable.
The implementation in Java 10 achieves this by making var
a reserved type name rather than a keyword. This means that code that uses var as a variable, method or package name is not affected.
As well as the simple cases, this actually permits programming constructs that were not possible before. For example, javac
has always permitted a very limited form of type inference:
public class Test { public static void main(String[] args) { (new Object() { public void bar() { System.out.println("bar!"); } }).bar(); } }
and this edge case has been known in the Java community since before the arrival of Java 7.
The problem with this form of type inference is that it has no real practical applications - the type of "Object-with-a-bar-method" exists within the compiler, but the type is impossible to express as the type of a variable - it is not a denotable type.
With the arrival of Java 10, the type of variables does not need to be made explicit, and instead var
can allow us to preserve the static type information by avoiding denoting the type. This means we can now modify our example and write:
var o = new Object() { public void bar() { System.out.println("bar!"); } }; o.bar();
This use of var
as a "magic type" allows the programmer to preserve type information for each distinct usage of type inference in a way that is somewhat reminiscent of type constraints from Java's generics.
More advanced usages of var
with non-denotable types are possible, and while the feature is not able to satisfy every criticism of Java's type system, it does represent a definite (if cautious) step forwards.