Java 10 released!

There is a bunch of new features in this release, but I have a clear personal favourite: local variable inference. Just have a look:

1
var obvious = 1;

What is the type of obvious?

Over time we evolved a bit:

1
2
3
4
List jdk4 = new ArrayList();
List<String> jdk5 = new ArrayList<String>();
List<String> jdk7 = new ArrayList<>();
var jdk10 = new ArrayList<>();

There is, naturally, a catch: where are the generics in the last example? I think it’s fair not to expect the compiler to use a crystal-ball.jar, so

1
var generics = new ArrayList<String>();

Generics will be a

1
ArrayList<String>();

now, as expected. One last catch – this will trigger IntelliJ et al to suggest returning an ArrayList instead of List, but I guess this will get better over time.

Author: ags

bio

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.