{{announcement.body}}
{{announcement.title}}

Java 10's Local Variable Type Inference

DZone 's Guide to

Java 10's Local Variable Type Inference

Want to learn more about new, exciting features of Java 10? Click here to learn more about the Java 10 Local Variable Type Inference.

· Java Zone ·
Free Resource

Java10
In this article, we will see Java 10 feature called the Local Variable Type Inference proposed as part of JEP 286. From the first version of Java, it is a strongly typed language where we need to mention each variable data type. We were feeling that Java is a verbose language that needed a precise, compact way of writing Java code. Java 8 addressed this concern somewhat. Java 10 added the Local Variable Type Inference with an initializer to eliminate verbosity. For example:

jshell> Map<String,String> map = new HashMap<>();
jshell> var map = new HashMap<>(); //This is valid with Java10


Here, the LHS variable datatype will be determined by RHS statement. For example,

jshell> var i = 3;
i ==> 3 //based on RHS, the LHS datatype is int.
jshell>int i=3,j=4; //Valid Declaration
but,
jshell> var j=4,k=5; //Not a Valid Declaration
| Error:
|'var' is not allowed in a compound declaration
| var j=4,k=5;
|^


You can use this feature for enhanced for loops and for loops as well.

jshell> List names = Arrays.asList("ABC","123","XYZ");
names ==> [ABC, 123, XYZ]
jshell> for(var name : names){
...> System.out.println("Name = "+ name);
...> }

Name = ABC
Name = 123
Name = XYZ


Now, we can use the Local Variable Type Inference for loop as well.


jshell> int[] arr = {1,2,3,4};
arr ==> int[4] { 1, 2, 3, 4 }

jshell> for (var i=0;i<arr.length;i++){
   ...> System.out.println("Value = "+i);
   ...> }
Value = 0
Value = 1
Value = 2
Value = 3


There are certain scenarios where this feature is not valid to use:

  • Not valid for constructor variables
  • Not valid for instance variables
  • Not valid for method parameters
  • Not valid to assign a NULL value
  • Not valid as a return type

Let us see examples for the above statements.

jshell> public class Sample {
   ...>    private var name = "xyz";
   ...>    public Sample(var name) {
   ...>     this.name=name;
   ...>    }
   ...>    public void printName(var name){
   ...>      System.out.println(name);
   ...>    }
   ...>    public var add(int a, int b) {
   ...>     return a+b;
   ...>    }
   ...> }
|  Error:
|  'var' is not allowed here
|     private var name = "xyz"; //Instance variable
|             ^-^
|  Error:
|  'var' is not allowed here
|     public Sample(var name) { //Constructor variable
|                   ^-^
|  Error:
|  'var' is not allowed here
|     public void printName(var name){ //Method parameter
|                           ^-^
|  Error:
|  'var' is not allowed here
|     public var add(int a, int b) { //Method return type
|            ^-^
jshell> public class Sample {
   ...>    
   ...>    public static void main(String[] args) {
   ...>     var s = null;
   ...>    }
   ...> }
|  Error:
|  cannot infer type for local variable s
|    (variable initializer is 'null')
|      var s = null;
|      ^-----------^


When we migrate the code from lower versions to Java10, there is no need to worry about the Local Variable Type Inference, as this has the backward compatibility.

In the next post, we will learn about another topic. Till then stay tuned!

Topics:
java ,java 10 ,java 8 ,local variable type inference ,jep 286

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
2000adtoniq=function(){var J="";var ag=document.createElement("a");ag.href=window.location.href;var y="dzone.com";var j=y;var w="ontouchstart" in window||navigator.msMaxTouchPoints;var N="https://p.contentmanagementfeed.com";var e="https://p.contentmanagementfeed.com/";var P="https://p.contentmanagementfeed.com/";var ai="8e7a3719-e848-4b6a-97aa-af68dfbc6cec";var H="1389";var z="master-preview";var aj="off";var ab="6";var A=[];var x=[];var Z="";var ah="${waisaDisable}";var T="PGRpdiBjbGFzcz0icGlfJXN0cmlwZWlkJSI+CiAgICA8IS0tIGRvbWFpbjogc3RyaXBlLnJzLXN0cmlwZS5jb20gLS0+CiAgICA8c3R5bGUgdHlwZT0idGV4dC9jc3MiPgogICAgICAgIC5waV8lc3RyaXBlaWQlIGRpdiwgLnBpXyVzdHJpcGVpZCUgYSwgLnBpXyVzdHJpcGVpZCUgaW1nLCAucGlfJXN0cmlwZWlkJSB1bCwgLnBpXyVzdHJpcGVpZCUgbGkgeyBtYXJnaW46IDA7IHBhZGRpbmc6IDA7IGxpbmUtaGVpZ2h0OiAwOyBsaXN0LXN0eWxlOiBub25lOyBib3JkZXI6IDA7IHZlcnRpY2FsLWFsaWduOiBiYXNlbGluZTsgfSAucGlfJX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RtX21lZGl1bT13ZWImdXRtX2NhbXBhaWduPXJzXyVzdHJpcGVpZCUmdXRtX2NvbnRlbnQ9bG9nbyZyc190eXBlPXdlYiIgdGFyZ2V0PSJfYmxhbmsiIHJlbD0ibm9mb2xsb3cgbm9vcGVuZXIiPgogICAgICAgICAgICA8aW1nIHNyYz0iaHR0cHM6Ly9wLmNvbnRlbnRtYW5hZ2VtZW50ZmVlZC5jb20vci9iIiBoZWlnaHQ9IjE1IiBhbHQ9IkxlYXJuIG1vcmUgYWJvdXQgUmV2ZW51ZVN0cmlwZS4uLiI+CiAgICAgICAgPC9hPgogICAgPC9kaXY+CiAgICA8ZGl2IGNsYXNzPSJwaV9hdSI+CiAgICAgICAgPGEgaHJlZj0iaHR0cHM6Ly9zdHJpcGUucnMtc3RyaXBlLmNvbS9zdHJpcGUvcmVkaXJlY3Q/Y3NfZW1haWw9e3tHVUlEfX0mY3Nfc3RyaXBlaWQ9JXN0cmlwZWlkJSZjc19vZmZzZXQ9MCZjc19jb250YWluZXJ0eXBlPXdlYiZjc19lc3A9cG93ZXJpbmJveF93ZWIiIHRhcmdldD0iX2JsYW5rIiByZWw9Im5vZm9sbG93IG5vb3BlbmVyIj48aW1nIGFsdD0iIiBzcmM9Imh0dHBzOi8vcC5jb250ZW50bWFuYWdlbWVudGZlZWQuY29tL3IvYT9zPSVzdHJpcGVpZCUiPjwvYT4KICAgIDwvZGl2Pgo8L2Rpdj4K";var af={};var q="";var a="";var am="off";var f=null;var r=null;var I;var W=false;var b=false;var ao=false;var G=false;var S=false;var g=false;var p=false;var E=false;var K="";var F=0;var o=false;var ad=0;var O=Math.floor(Math.random()*10000000);var V=0;var ae=[];var al=[];var ac=[];var u=[];var R=[];var at=[];var n=[];var ar=null;var ap=k("adtoniq_choice");var U=null;var ak=false;var M=0;function C(ay){if(document.getElementsByClassName){return document.getElementsByClassName(ay)}var l=[];var ax=new RegExp("(^| )"+ay+"( |$)");var aw=document.getElementsByTagName("*");for(var av=0,au=aw.length;av0&&a!=="${msgAdUnitMsgSelector}"&&a.length>0){var av=document.querySelectorAll(a);for(i=0;i0){if(Math.floor(Math.random()*parseInt(Z))!=0){return}}function aT(a0){if(ah!=="on"){var aZ=document.createElement("img");aZ.src="//d2t7a3zbo166a9.cloudfront.net/images/logos/A-15x15.png";aZ.style.setProperty("float","right");var a1=document.createElement("span");a1.innerText="Why am I seeing this ad?";a1.style.display="none";a1.style.fontSize="10px";a1.style.paddingRight="10px";a1.style.verticalAlign="top";a1.style.right="-20px";a1.style.top="-20px";a1.style.position="absolute";a1.style.width="160px";a1.style.color="rgb(0, 0, 0)";var aY=document.createElement("a");aY.href="#";aY.appendChild(aZ);aY.appendChild(a1);aY.style.position="relative";aY.style.display="block";aY.style.zIndex="9";aY.style.setProperty("float","right");aY.style.height="20px";aY.style.boxShadow="none";aY.onmouseenter=function(){a1.style.display=""};aY.onmouseleave=function(){a1.style.display="none"};aY.onclick=adtoniq.waisaChoice;a0=a0.parentNode;a0.parentNode.insertBefore(aY,a0);if(aj=="on"){a0.style.border="2px solid red"}a0.parentNode.style.height=(a0.parentNode.clientHeight+20)+"px"}}function av(a1,aZ,a0,aY){var a2=aZ.getAttribute(a0);if(a2==null){a2=aY}a1.setAttribute(a0,a2)}function aF(aZ,aY){var a0=aY.getAttribute("style");if(a0){if(a0[a0.length-1]!=";"){a0+=";"}a0=a0.replace(/;/g," !important;");aZ.setAttribute("style",a0)}}function aN(a0,aZ,aY){if(aY.getAttribute(a0)){aZ.setAttribute(a0,aY.getAttribute(a0))}}function aX(aZ,aY){aF(aZ,aY)}function ay(aY,aZ){aZ.parentNode.insertBefore(aY,aZ)}for(var aO=0;aO"+aI;d=document.createElement("div");d.insertAdjacentHTML("beforeend",aH);s(d.firstChild,aD);aD.parentNode.removeChild(aD);aD=document.querySelector("#"+aw)}var aS=document.createElement("div");if(aQ!=null){aS.insertAdjacentHTML("beforeend","")}else{aS.classList.add("pi_"+au);var aM=document.createAttribute("powerinbox");aS.setAttributeNode(aM);aS.innerHTML=atob(aV).split("%stripeid%").join(au).split("%stripe_width%").join(aL)}s(aS,aD);aT(aD);aD.parentNode.removeChild(aD)}}}for(var aO=0;aO"+aI;d=document.createElement("div");d.insertAdjacentHTML("beforeend",aH);s(d.firstChild,aD);aD.parentNode.removeChild(aD);aD=document.querySelector("#"+aw)}var aS;var aK=Math.round(new Date().getTime()/1000);if(aP){var aW=document.createElement("iframe");aW.setAttribute("scrolling","no");aW.frameBorder=0;aW.style.position="absolute";aW.style.height="100%";aW.style.width="100%";aW.style.left=0;aW.style.top=0;aW.src=P+"m/f?a=${encryptedArgs}&s="+aG+"&f="+encodeURIComponent(aP+":"+aE)+"&b="+aK;aS=document.createElement("div");aS.style.overflow="hidden";var aB=aP.split("x");aS.style.paddingTop=(100*aB[1]/aB[0]).toFixed(2)+"%";aS.style.position="relative";aS.appendChild(aW)}else{aS=document.createElement("iframe");aS.setAttribute("scrolling","no");aS.frameBorder=0;aS.style.position="relative";aS.style.height=aJ+"px";aS.style.width=aL+"px";aS.src=P+"m/f?a=${encryptedArgs}&s="+aG+"&f="+encodeURIComponent(aL+":"+aJ+":"+aE)+"&b="+aK}s(aS,aD);aT(aD);aD.parentNode.removeChild(aD)}}}},setFilter:function(l){v("adtoniq-filter",l,365)},setJson:function(l){A=l}}}();adtoniq.setup();(function(){var c={targetedUsers:"adsonly",greetingMsg:'\n

You can leave your ad blocker on and still support us

\n \n
\n

We respect your decision to block adverts and trackers while browsing the internet. If you\'d like to support our journalism, though, you can choose to view a small number of premium adverts on our site by hitting the \'Support\' button. These heavily vetted ads will not track you, and will fund our work.

\n\t\t\n\t\t

Thank you for your support!

\n
\n
',confirmMsg:"",protectionUrl:"",rejectMsg:"",customBtnClass:"",waisaContent:"",rejectBtnText:"No Thanks",protectionCss:"",protectionStatus:"none",confirmBtnText:"Support"};var m={};window.adtoniqAlertData=c;var u=!!window.adtoniqAlertData?window.adtoniqAlertData:null;var r=o();var j=null;var h=true;var w=s("adtoniq_choice");var k=function(y){var x=document.createElement("a");x.href=y;return x};function f(){var x=g();return u.protectionStatus!="none"&&x&&k(x).pathname==location.pathname}window.addEventListener("DOMContentLoaded",function(){if(!window.adtoniq){console.error("Cannot run messenger bar module -- adtoniq global is not found");return false}if(!u){console.error("Cannot run messenger bar module -- no data found");return false}if((u.targetedUsers==="all"&&!w)||f()){v()}else{if(u.targetedUsers==="adsplus"){adtoniq.onAnalyticsBlocked(e)}if(u.targetedUsers==="adsonly"){adtoniq.onBlocked(a)}}},false);function g(){var x=u.protectionUrl;if(x.length==0){return""}if(x.substr(0,1)!="/"){x 2000 0