Java 10 – JEP 286: Local-Variable Type Inference
Java 10 is around the corner with RC Build available here. The features targetted for this release can be found here. Of all the JEPs targetted for Java 10, the interesting and most talked about by the developer community is the 286: Local-Variable Type Inference.
What is Local Variable Type Inference?
We saw in Java 8, the Diamond operator which allowed us to write
List<Map data = new ArrayList<Map();
as
List<Map data = new ArrayList();
where the type on the RHS was inferred based on the type on the LHS. This did work albeit in anonymous inner classes which was fixed in Java 9.
Java 10 goes a step further and says, the below:
List<Map data = new ArrayList();
can be written as:
var data = new ArrayList();
Local Variable type inferencing allows the developer to skip the type declaration associated with local variables (those defined inside method definitions, initialization blocks, for-loops and other blocks like if-else) and the type is inferred by the JDK.
Where it can be used?
Let me create a sample class to show the different ways to use Local variable inference var
:
public class LegalLocalVarInferenceDemo{ //in a static/instance initialization block static { var anotherName = "Sanaulla"; System.out.println("Hello, " + anotherName); } public static void main(String[] args){ //as a local variable var name = "Mohamed Sanualla"; System.out.println("Hello " + name); var data = new ArrayList(); data.add(Map.of("key1", "value1", "key2", "value2")); data.add(Map.of("key11", "value11", "key22", "value22")); data.add("hello"); System.out.println(data); System.out.println("********** As iteration variable in enhanced for-loop ***************"); for ( var object : data){ System.out.println(String.format("%s of type %s", object, object.getClass().getName())); } System.out.println("********** As looping index in for-loop ***************"); for ( var i = 0 ; i < data.size(); i++ ){ var object = data.get(i); System.out.println(String.format("%s of type %s", object, object.getClass().getName())); } System.out.println("********** As a return value from another method ***************"); var anInteger = someAnotherMethod(); System.out.println("someAnotherMethod returned " + anInteger); } //As a return value in a method public static Integer someAnotherMethod(){ System.out.println("someAnotherMethod called"); var returnObj = 12; return returnObj; } }
Where it cannot be used?
Let me create another sample class which shows how var
cannot be used:
public class IllegalLocalVarInferenceDemo{ //Not permitted in class fields //var someProperty; //Not allowed as parameter for constructor // public LocalVarInferenceDemo(var param1){ // } public static void main(String[] args){ //Not allowed in catch parameter // try{ // //some operations // }catch(var ex){ // } } //Not permitted below //public static void someMethod(var param1, var param2){ // System.out.println("Some method called"); //} //Not permitted in method return type // public static var someAnotherMethod2(){ // System.out.println("someAnotherMethod called"); // var returnObj = 12; // return returnObj; // } }
Sample Code
This sample can be found on GitHub
Published on Java Code Geeks with permission by Mohamed Sanaulla, partner at our JCG program. See the original article here: Java 10 – JEP 286: Local-Variable Type Inference Opinions expressed by Java Code Geeks contributors are their own. |
List<Map data = new ArrayList(); ?
I don't think it is possible. This code does not compile. I wonder what javacodegeeks is about to become?
Thanks for notifying. Its a typo. I will fix it on my blog post.
Actually a typo which occurred due to the blog engine stripping of the characters