Bethan Palmer Bethan is a Java developer and a Java Champion. She has spoken at conferences including JavaOne/Code One, DevFest and NetBeans days. She has a degree in English Literature.

Java 10 Local-Variable Type Inference Explained in 5 Minutes

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).

For example, with Java 9 or earlier your code might look like this:

String greeting = "Hello World";
ArrayList<String> messages = new ArrayList<String>();
Stream<String> stream =;

But in Java 10 you can do this:

var greeting = "Hello World";
var messages = new ArrayList<String>();
var stream =;

Where you can use it:

  • local variables with initialisers
  • indexes in for each loops
  • locals in for loops

Where you cannot use it:

  • method formals
  • constructor formals
  • method return types
  • fields
  • catch formals

Basically, the only time you can use var is when initialising local variables and in for loops.

What are the pros?

Var helps to reduce the amount of boilerplate code needed to write Java. It makes the language less verbose, and the information about type is not duplicated.

For example in Java 7 you would do:

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

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.

Are you a Developer working with PDF files?

Our developers guide contains a large number of technical posts to help you understand the PDF file Format.

Do you need to solve any of these problems?

Display PDF documents in a Web app
Use PDF Forms in a web browser
Convert PDF Documents to an image
Work with PDF Documents in Java
Bethan Palmer Bethan is a Java developer and a Java Champion. She has spoken at conferences including JavaOne/Code One, DevFest and NetBeans days. She has a degree in English Literature.

5 Replies to “Java 10 Local-Variable Type Inference Explained in 5 Minutes”

  1. Good explanation.

    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?

    1. Hi Sanjay,
      Using var does make the code less explicitly typed, but I think it is worth the trade-off when considering how much cleaner the code looks

  2. Some developers, not wanting to specify all types (HashMap<HashSet, ArrayList>) just use HashMap and cast when retrieving results.
    In this case, using var would ensure casting is not used and we woul dhave more type safety IIUC.

  3. In the above post it says we can use the following
    `var map = new HashMap();`

    How does compiler know the type of the map key and map value, is it based on what i try to set in the hash map ?
    If is try
    map.put(“test_key”, 123);
    map.put(“test_key_1”, “test_value”);

    will the compiler throw a type error ?

    Thanks in advance for who ever can answer my question

Comments are closed.