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.

Are you a Java Developer working with PDF files?

Find out why you should be using JPedal

5 things we learned about var in Java at Code One

2 min read

Since local variable type inference was introduced in Java 10, there has been a lot of debate, and a lot of people don’t like it:

At Code One this year there have been loads of talks about var, most notably by Venkat Subramaniam and Stuart Marks. My key takeaways are that sometimes it makes code less readable, and it is probably not worth spending time refactoring old code to use var. But when writing new code, it can be a nice way to remove boilerplate code and in some cases can actually improve readability:

var hashMap = new HashMap<String, Integer>();

Here are 5 points about var that came up at Code One:

1. Java already had type inference before Java 10

There has been a lot of talk about the introduction of type inference in Java 10. But type inference is not actually new to Java. Since Java 8 came out in 2014, we have been able to use type inference in lambdas. For example:

BinaryOperator<Integer> add = (x, y) -> x + y;

In this example x and y are integers, but we never explicitly say that they are – the compiler infers this for us.

It is only the ability to use type inference for local variables that is new in Java 10.

 

2. Var is not a reserved keyword in Java

Var is not reserved. So if you really wanted to annoy your colleagues you could put this in your code:

var var = 7;

And the code will compile and run.

3. You can write bad code with var, but you can write bad code without var

One of the most common complaints about var is that it will allow programmers to write unreadable code.

This is definitely possible, for example it would be a bad idea to do something like this:

var x = getSomething();

When you read this line of code there is no way to tell what type x is.

But it might be a better idea to teach someone not to do this than to not use the feature. It can still be a good idea to use var when the right hand side is a method call.

An example of a good place to use var would be here:

var inputStream = socket.getInputStream();

You can probably guess from looking at the context that inputStream is of type InputStream.

4. Var is not a move towards dynamic typing

Java is still very much statically typed and the introduction of var is not a move towards dynamic typing.

Even though the type is inferred, there is still a static type. When I say:

var x = 7;

the variable x is not of a type called ‘var’. Is it still a variable of type int, and it is still statically typed. We are just telling the compiler to work out the type from the context. This is very different to ‘var’ in Javascript.

So Java is not moving towards becoming a dynamically typed language, and is not turning into Javascript.

5. In Java 11 you can use var in lambda expressions

In Java 10 you can do this:

BinaryOperator<Integer> add = (x, y) -> x + y;

But in Java 11 you can do this:

BinaryOperator<Integer> add = (var x, var y) -> x + y;

But why would you do that? One reason is if you want to add annotations, you need to specify the type. E.g. you can do:

BinaryOperator<Integer> add = (final var x, final var y) -> x + y;

You would not be able to add ‘final’ without the var.

 

Are you using var yet? What do you think of it?

 



Our software libraries allow you to

Convert PDF to HTML in Java
Convert PDF Forms to HTML5 in Java
Convert PDF Documents to an image in Java
Work with PDF Documents in Java
Read and Write AVIF, HEIC, WEBP and other image formats
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.

2 Replies to “5 things we learned about var in Java at…”

  1. The type of a value is derived using literals and they are part of Java language since its inception.
    float f = 3.14; // is reported as error by compiler. Because 3.14 is a double.

    I don’t understand why so much hullabaloo about a feature that was already part of Java language. Isn’t its just marketing old wine in new bottle!

    1. Hi Mohammed,
      Yes that’s true, Java could already work out the type from the right hand side of the assignment. The biggest difference with using var is the readability.

Comments are closed.