Skip to content

macedoj/java-var-type-inference

Repository files navigation

New features from Java 10 and 11

Code examples to demonstrate the new features from Java 10 and 11 release.

New Features

Local Variable Type Inference

The LocalVariableTypeInference.java class have some code examples to demonstrate the Java 10 capability to infer the local variable type.

Code exemples:

 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

License (MIT)

Copyright 2018, Juliano Macedo. See LICENSE file for details.

About

Code examples to demonstrate Java VAR feature

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages