var keyword in Java


Java is a statically-typed language known for its verbosity and strict type checking. However, with the release of Java 10, a new feature called Local-Variable Type Inference was introduced, bringing the var keyword to the language and changing the way Java developers code. This article will explore the var keyword, illustrating its use cases and discussing its implications for Java coding practices.

Understanding 'var' in Java

In Java, traditionally, we needed to explicitly declare the type of every variable we created. With the introduction of var in Java 10, this has changed. The var keyword allows you to declare a local variable without specifying its type. The Java compiler will infer the type of the variable from its initializer.

var name = "Java";
var version = 10;
var list = new ArrayList<String>();

In the above example, name is inferred to be of type String, version is int, and list is ArrayList. Note that var can only be used to declare local variables inside methods, and in for-loop and try-with-resources statements.

Using 'var' for Cleaner Code

The use of var leads to less verbose and cleaner code, especially when dealing with complex generic types. For example, consider the following line of code without var −

Map<String, List<Map.Entry<String, Integer>>> map = new HashMap<>();

This can be significantly simplified using var −

var map = new HashMap<String, List<Map.Entry<String, Integer>>>();

The type of the map variable is inferred by the Java compiler from its initializer, reducing redundancy and enhancing code readability.

'var' and Collections

When working with collections, var shines by reducing verbosity, particularly with generics. Suppose we have a list of strings −

var names = List.of("Alice", "Bob", "Charlie");

The type of the names variable is inferred to be List. It's important to note that while var improves readability by reducing verbosity, the type information isn't lost - the variable still has a static type, determined at compile time.

'var' in Loops

The var keyword can also be used in enhanced for-loops, which can be useful when working with complex generic types −

var map = new HashMap<String, Integer>();
// populate map
for (var entry : map.entrySet()) {
   // process entry
}

Here, entry is inferred to be of type Map.Entry<String, Integer>.

Limitations of 'var'

While var is a powerful addition to Java, it's essential to be aware of its limitations −

  • var can only be used to declare local variables. It can't be used for field declarations, method parameters, or return types

  • var requires an initializer. You can't declare a var variable without initializing it because the compiler wouldn't be able to infer the variable's type.

  • var can't be used with null initializers, as the type can't be inferred.

  • var can't be used to declare variables of an array type, but it can be used to declare a reference to an array.

var arr = new int[10]; // correct
var[] arr = new int[10]; // incorrect

Conclusion

The var keyword is a welcome addition to the Java language, enhancing code readability and reducing verbosity. It makes the code cleaner, especially when working with complex generic types. Despite this, it's important to remember that var does not make Java a dynamically typed language. The type of the var variables is still statically checked at compile time.

Updated on: 19-Jul-2023

350 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements