Bethan Palmer FollowBethan is a Java developer and product manager for JPedal at IDRsolutions. She has spoken at conferences including JavaOne and NetBeans day and has a degree in English Literature.

Java 10 Local-Variable Type Inference Explained in 5 Minutes

April 5, 2018 1 min read

The biggest new feature in Java 10 is the introduction of the Local-Variable Type Interface.

What is Local-Variable Type Inference?

This new feature will allow you to declare and initialise local variables with var, rather than specifying a type. This feature is already a part of most other statically typed languages (It has been in C# since 2007).

In Java 8 the diamond operator was introduced and this was shortened to:

HashMap<HashSet<String>, ArrayList<String>> map = new HashMap<>();

In Java 10 you can now do:

var map = new HashMap<>();

What are the cons?

It could be argued that using var makes code less readable, as it may not be immediately obvious what the type is. In the example above the code is much more concise, but it is also less clear. You have to be more careful about giving variables names that make it obvious what they are for. And when you do not specify the type, you are essentially leaving it down to someone else to work out what it is.

Are you using var yet? Let us know your thoughts in the comments.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (Twitter, Facebook and Google+) or the Blog RSS.

Bethan Palmer FollowBethan is a Java developer and product manager for JPedal at IDRsolutions. She has spoken at conferences including JavaOne and NetBeans day and has a degree in English Literature.

But I think this is going backward when Java introduced the concept of mentioning the type of arguments for collections. Like Map kvm = new HashMap() to make it type safe. With introduction of var (even for local variables) they are some somewhat going backwards. Isn’t it?