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.
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()
To ensure TypeInference from Java 10, the LocalVariableTypeInference.java
class
are fully reviewed by unit tests, that can be found in class HaversineAlgorithmTest.java
.
@Test
void localVariableTypeInferenceArrayList() {
var optional = variableTypeInference.localVariableTypeInferenceArrayList();
var someObject = optional.isPresent() ? optional.get() : Optional.empty();
assertAll("localVariableTypeInference",
() -> assertTrue(someObject instanceof ArrayList),
() -> assertNotEquals(someObject, null)
);
}
Just open a terminal and paste the commands below:
gradle clean build --stacktrace
gradle clean build cleanidea idea --stacktrace
gradle clean build cleaneclipse eclipse --stacktrace
Copyright 2018, Juliano Macedo. See LICENSE file for details.