var Vs val in Java 11
var is a new reserved word that brings type inference to the strongly typed Java programming language. The inference algorithm is very restricted and prevents the inference of mutable variables.
This feature is similar to the var in Groovy, Scala, and Kotin. It also simplifies the syntax for method block initializers and lambda expressions.
While var reduces the amount of ceremony around the declaration of local variables, it does not sidestep the rules about strong typing. The type inference that happens when you use var is done at compile time and is put into the bytecode. This means that any code that calls the variable can still catch an exception if the type is not correct.
This also means that you cannot use var for declarations of field types, method return types, constructor formal variables, or in the type of an argument to a method call. However, it is possible to declare a var variable in an initializer expression and make the type explicit there.
There is some controversy about this, with some developers welcoming the concision and others worrying that it will deprive readers of important type information and impair readability. Ultimately, it’s up to the developers themselves to decide how best to write their code. Those who have come from other Java-like languages with var will probably want to keep using it, for example.
The var reserved word allows the Java compiler to infer the type of a local variable based on how the variable is initialized. This can reduce the number of declarations in a code block and make it easier for programmers to read the code. However, it does not sidestep Java’s strong typing rules and it cannot be used for method parameters or fields declarations.
There was a lot of debate over whether to introduce var for local variables, as in Groovy or Scala, or to support val for immutable local variables. In the end, var was chosen because it offers more flexibility without introducing new syntax.
var also makes it easier to write lambdas with fewer type declarations. This will make the code more readable and improve the onboarding experience for new developers. You can learn more about this feature in Sander’s Pluralsight course What’s New in Java 10. Click here to get a free 10-day trial.
If you want a variable to remain constant throughout a function, val is the best option. This helps to prevent the need for an elaborate type check that can lead to confusion and errors. Moreover, the use of val can reduce the length of code and make it easier to understand and maintain.
However, there are situations in which var is a good choice. In Kotlin, for instance, var can be used for values that are mutable, such as user sessions or dynamic configurations. It is also useful for loops where the value may need to change over iterations.
While var can be used to declare local variables, it cannot be used for class variables or method formal parameters. It also cannot be used for the return type of methods. Moreover, var is not compatible with the const keyword. Attempting to assign a var variable to a const variable will result in an error. Furthermore, var is not supported in block constructs such as try…catch and switch statements.
Many IDEs offer a feature that extracts the static type of local variables and writes it out for you. This is similar to var but differs in that it removes the need to write out the type explicitly.
The var keyword makes Java code easier to read, especially for other developers who will be reading it. It also reduces the boilerplate required to create local variables and can help newer developers get up to speed more quickly.
However, it is important to note that var does not provide the same level of safety as val or let and it should never be used in place of those types. It is a convenience feature, not a replacement for other methods for declaring immutable local variables.