Local Variable Type Inference
Th local variable type inference is a feature released in Java 10, that enable to developers the possible to skip type declaration when implement local variables.
Code examples
The LocalVariableTypeInference.java
class have some code examples to demonstrate
the Java 10 capability to infer the local variable type.
var list = new ArrayList<String>(); // infers ArrayList<String>
var hashMap = new HashMap<String, String>(); // infers HashMap<String, String>
var stringBuilder = new StringBuilder(); // infers StringBuilder()
Unit test
To ensure TypeInference from Java 10, the LocalVariableTypeInference.java
class
are fully reviewed by unit tests, that can be found in class HaversineAlgorithmTest.java
.
Unit test example:
@Test
void localVariableTypeInferenceArrayList() {
var optional = variableTypeInference.localVariableTypeInferenceArrayList();
var someObject = optional.isPresent() ? optional.get() : Optional.empty();
assertAll("localVariableTypeInference",
() -> assertTrue(someObject instanceof ArrayList),
() -> assertNotEquals(someObject, null)
);
}
Run the code
Just open a terminal and paste the commands below:
gradle clean build --stacktrace
For Intellij IDEA
gradle clean build cleanidea idea --stacktrace
For Eclipse IDE
gradle clean build cleaneclipse eclipse --stacktrace
License
Copyright 2018, Juliano Macedo. See LICENSE file for details.