Help the world stop coronavirus! Stay home!

Prev Next

Java / Java Identifiers

Explain var keyword in Java.

var keyword is introduced in Java 10 to enhance the Java Language to extend type inference to declarations of local variables with initializers.

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

var is not a keyword in Java, but rather it is a reserved type name. var cannot be used for fields, return types, class names, or interface names.

var can only be used for implicit type inference in the following contexts.

  • local variables with initializers.
  • indexes in the enhanced for-loop.
  • locals declared in a traditional for-loop.

Since var is a reserved type name, it can still be used for package name, method name, and variable name along with its new type-interference role. For example, the following are all examples of valid uses of var in Java.

var map = new HashMap<String, Integer>();
var k= 0;
var var = 10;
for (var i = 0; i < 100; i++) { /* ... */ }
public int var() { return -1; }
package var;

❤Cash Back At Stores you Love !!!❤

Earn your $10 reward when you make your first purchase through Ebates by signing up with clicking below button.

Ebates Coupons and Cash Back

More Related questions...

Show more question and Answers...


Comments & Discussions