What is 'var' in Java 10, and is it comparable to JavaScript?
Asked Answered
K

3

6

I was reading about the new features released in Java 10 and I found this:

Java 10 introduced var as a reserve type name to reduce verbosity. It can be used as a variable, method, and package name, but we cannot use it as a class or interface name.

Is var similar to the var keyword in JavaScript? What exactly are the similarities and the differences between Java's var and JavaScript's var?

Knotts answered 3/4, 2018 at 18:36 Comment(0)
D
25

var serves a somewhat conceptually similar purpose in both languages, but there are important differences.

JavaScript is a dynamically typed language. A variable does not have type information. It does not have restrictions on what it can contain. It is just a holder for a value, and the value can be of any type.

Java is a statically typed language and the opposite is true for all of those statements. A programmer has to say up-front what type their variable can hold. They may be as lenient as Object (every class extends Object implicitly) or as specific as FieldTagTestsVisitorComparator.

Object objectVariable = "hello";
FieldTagTestsVisitorComparator enterpriseVariable = new FieldTagTestsVisitorComparator();

In JavaScript, I can very concisely write

var age = 10;

and I have a place in which to store a value which I can read or manipulate later.

If I decide later in my code that I want to reassign something else to this variable then I can do so.

age = {years: 10, months: 4, days: 5};

You can see clearly see which declarations are nicer to work with. People often complain about Java's verbosity. In my example above, I've declared FieldTagTestsVisitorComparator twice on the same exact line. What value is that adding? I've just repeated myself.

var in Java seeks to reduce the amount of repeated declarations. Like the diamond operator before it, the language designers are trying to improve the Java compiler so that it is clever enough to make inferences about what programmers are intending. Why should something be explicit if it can be implicit?

What this means in practice is that the type of the variable on the left-hand-side of the assignment is deduced from the right-hand-side.

If I write the following in Java

var foo = new StringBuilder();

then the Java 10+ compilers are clever enough to recognize a string builder on the right-hand-side and to automatically give the variable foo the appropriate type.

This fundamentally differs from JavaScript because although I have not specified the type, a type is still present. I cannot subsequently assign anything I like to it:

foo = 3; // fails to compile, foo is of type StringBuilder

var is purely used at compile time. The compiler works out the type, replaces it, and the bytecode is the same as if it had never been used. There is no functional difference between

var foo = new StringBuilder();

and

StringBuilder foo = new StringBuilder();

It is purely a cosmetic difference which makes the language more expressive.


A note about var's status

Java language designers are always very careful when adding new features to not break backwards compatibility, and so var has somewhat special status. It is not a keyword, because having keyword status would be overly restrictive. Keywords cannot be used as identifiers, for example; I cannot have a method called if because if is a keyword.

var is, as your quote states, a "reserved type name". This sounds complex at first, but it is actually very descriptive of var's status. It is the name of a type that has been reserved by Java. You cannot name your types - classes, interfaces, enumerations - var, because that would introduce an ambiguity.

You are still free to use the word "var" in every other context - method names, variable identifiers, etc.

By having this status, the designers have maximized backwards compatibility (short of not adding the feature at all!). Unless you had a class called lowercase var -- and firstly what are you doing choosing these terrible names, and secondly why didn't you at least follow the naming convention and call it Var? -- then you're protected from any breaking changes.

There's a whole question about this here: What type of token exactly is "var" in Java 10?


Good feature, right? It definitely is, but it important not to simply start using it everywhere just because you can. Stuart Marks, one of the JDK developers, has some excellent style guidelines which I recommend that you read.

My rule of thumb is that if the type is not specified on the same line (as it is in the above examples) then I do not use var. The compiler is clever enough to infer the type from the result of any valid expression, so the following is perfectly valid:

var foo = bar();

However, without looking at the method signature, it is not immediately obvious what the type of the variable will be inferred to be. There isn’t any ambiguity for the compiler, but it is less clear for the human who's reading it. In such a case, we have sacrificed readability for brevity.


Further reading

Drice answered 2/10, 2019 at 18:51 Comment(0)
V
3

var is something that is already in C#. Basically, instead of declaring a variable type, it assumes its type from what it is being set to. For instance:

var myString = "Hello world"

Because you are setting it to a value in quotes, it assumes it is a String, rather than having to specify like:

String myString = "Hello world"
Voyageur answered 3/4, 2018 at 18:38 Comment(1)
var is not a keyword in Java. If it was we wouldn't be able to use it as var var = 1;Staten
H
-1

var is a new keyword that was introduced in Java 10. The behaviors are almost the same as JavaScript. See below for what we can and cannot with the var keyword in Java.

Can

  • Declare any datatype with the var keyword
  • Be used in a local variable declaration

Can not

  • Be used in an instance and global variable declaration
  • Be used as a generic type
  • Be used with the generic type
  • Be used without explicit initialization
  • Be used with a lambda expression
  • Be used for method parameters and return type

var supports generic types and also lambda expressions from Java 11.

Hermitage answered 29/11, 2021 at 13:29 Comment(4)
Let's use it as a generic type: var stringList = List.of("foo"). Works for me.Aerial
@JohannesKuhn, It's not a Generic Type. It's a String. var<var> al = new ArrayList<>(); this one will not work with varHermitage
It's List<String>, not String.Aerial
@JohannesKuhn, This question is about Java 10 🙂 but Yes, var supports Generic Type from Java 11. Updated my answer too. Thank you.Hermitage

© 2022 - 2024 — McMap. All rights reserved.