
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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
'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.