Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type of token exactly is "var" in Java 10?

Tags:

java

java-10

In the last issue of Heinz Kabutz's newsletter, #255 Java 10: Inferred Local Variables, it is shown that var is not a reserved word in Java 10, because you can also use var as an identifier:

public class Java10 {
    var var = 42; // <-- this works
}

However, you cannot use i.e. assert as an identifier, as in var assert = 2, because assert is a reserved word.

As it's told in the linked newsletter, the fact that var is not a reserved word is good news, because this allows code from previous versions of Java that uses var as an identifier to compile without problems in Java 10.

So, what's var then? It's neither an explicit type nor a reserved word of the language, so it's allowed to be an identifier, however it does have a special meaning when used to declare a local variable in Java 10. What exactly do we call it in the context of a local variable declaration?

Additionally, apart from supporting backwards compatibility (by allowing older code that contains var as an identifier to compile), are there other advantages to var not being a reserved word?

like image 687
fps Avatar asked Mar 03 '18 20:03

fps


People also ask

What type is VAR in Java?

Each statement containing the var keyword has a static type which is the declared type of value. This means that assigning a value of a different type will always fail. Hence, Java is still a statically typed language (unlike JavaScript), and there should be enough information to infer the type of a local variable.

Is var a variable in Java?

var can be used in a local variable declaration instead of the variable's type. With var , the Java compiler infers the type of the variable at compile time, using type information obtained from the variable's initializer. The inferred type is then used as the static type of the variable.

How do you use VAR in Java?

Java developers have needed to keep typing the class name explicitly for a long time. var keyword allows us to declare the local variable without type information supported by the power of type inference of Java compiler. We can simplify the following code. String str = "Hello, Java!"


2 Answers

According to JEP-286: Local-Variable Type Inference, var is

not a keyword; instead it is a reserved type name.

(Earlier versions of the JEP left room for implementing either as a reserved type name or as a context-sensitive keyword; the former path was ultimately chosen.)

Because it's not a "reserved keyword", it is possible to still use it in variable names (and package names), but not in class or interface names.

I would think the biggest reason for not making var a reserved keyword is backwards compatibility with old source code.

like image 161
Mick Mnemonic Avatar answered Oct 22 '22 02:10

Mick Mnemonic


var is a reserved type name var is not a keyword, It’s a reserved type name.

We can create a variable named “var”.

you can read here for more details.

var var = 5; // syntactically correct
// var is the name of the variable
“var” as a method name is allowed.

public static void var() { // syntactically correct 
}
“var” as a package name is allowed.

package var; // syntactically correct
“var” cannot be used as the name of a class or interface.
class var{ } // Compile Error
LocalTypeInference.java:45: error: 'var' not allowed here
class var{
      ^
  as of release 10, 'var' is a restricted local variable type and cannot be used for type declarations
1 error

interface var{ } // Compile Error

var author = null; // Null cannot be inferred to a type 
LocalTypeInference.java:47: error: cannot infer type for local variable author
                var author = null;
                    ^
  (variable initializer is 'null')
1 error
like image 7
Roushan Avatar answered Oct 22 '22 03:10

Roushan