Java 10 - Local Variable Type Inference
- schick09
- Oct 11, 2024
- 1 min read
Java 10 added a new feature designed to streamline your code to make it less verbose. You can now declare local variables without specifying the type. The type is inferred and therefore this is called Local Variable Type Inference.
For example, you can declare a String variable like so:
var hello = "Hello world!";
Or an integer primitive like this:
var i = 10;
A List can be declared as follows:
var list = List.of(1, 2, 3, 4, 5);
And so on.
The pros to this approach are less verbose code. However, teams should strive to be consistent and only use this option if it significantly shortens the code and it is very clear what the data type of the variable is.
Here's a link to our GitHub location to access an example of using local variable type inference: https://github.com/daveschickconsulting/java_10_examples/tree/main/src/funwithlocalvariabletypeinference

Comments