{"id":3244,"date":"2019-03-17T13:16:59","date_gmt":"2019-03-17T13:16:59","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3244"},"modified":"2019-03-17T16:20:25","modified_gmt":"2019-03-17T16:20:25","slug":"what-is-type-inference","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/what-is-type-inference\/","title":{"rendered":"What is Type inference"},"content":{"rendered":"<p>Type inference is a feature of Java which provides ability to compiler to look at each method invocation and corresponding declaration to determine the type of arguments.<br \/>\nJava provides improved version of type inference in Java 8.<br \/>\nHere, we are creating arraylist by mentioning integer type explicitly at both side. The following approach is used earlier versions of Java.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();  \r\n<\/pre>\n<p>In the following declaration, we are mentioning type of arraylist at one side. This approach was introduce in Java 7. Here, you can left second side as blank diamond and compiler will infer type of it by type of reference variable.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nList&lt;Integer&gt; list2 = new ArrayList&lt;&gt;();   \r\n<\/pre>\n<p>Improved Type Inference<br \/>\nIn Java 8, you can call specialized method without explicitly mentioning of type of arguments.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nshowList(new ArrayList&lt;&gt;());  \r\n<\/pre>\n<p><strong>Example<\/strong><br \/>\nYou can use type inference with generic classes and methods.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\npublic class TypeInferenceExample {\r\n public static void showList(List &lt; Integer &gt; list) {\r\n  if (!list.isEmpty()) {\r\n   list.forEach(System.out::println);\r\n  } else System.out.println(&quot;list is empty&quot;);\r\n }\r\n\r\n public static void main(String&#x5B;] args) {\r\n\r\n  \/\/ An old approach(prior to Java 7) to create a list  \r\n  List &lt; Integer &gt; list1 = new ArrayList &lt; Integer &gt; ();\r\n  list1.add(11);\r\n  showList(list1);\r\n\r\n  \/\/ Java 7    \r\n  List &lt; Integer &gt; list2 = new ArrayList &lt; &gt; (); \/\/ You can left it blank, compiler can infer type  \r\n  list2.add(12);\r\n  showList(list2);\r\n\r\n  \/\/ Compiler infers type of ArrayList, in Java 8  \r\n  showList(new ArrayList &lt; &gt; ());\r\n }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n11\r\n12\r\nlist is empty\r\n<\/pre>\n<p><strong>Type inference for Custom Classes<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nclass GenericClass &lt;X&gt; {\r\n X name;\r\n public void setName(X name) {\r\n  this.name = name;\r\n }\r\n public X getName() {\r\n  return name;\r\n }\r\n public String genericMethod(GenericClass &lt; String &gt; x) {\r\n  x.setName(&quot;John&quot;);\r\n  returnx.name;\r\n }\r\n}\r\n\r\npublic class TypeInferenceExample {\r\n public static void main(String&#x5B;] args) {\r\n  GenericClass &lt; String &gt; genericClass = new GenericClass &lt; String &gt; ();\r\n  genericClass.setName(&quot;Peter&quot;);\r\n  System.out.println(genericClass.getName());\r\n\r\n  GenericClass &lt; String &gt; genericClass2 = new GenericClass &lt; &gt; ();\r\n  genericClass2.setName(&quot;peter&quot;);\r\n  System.out.println(genericClass2.getName());\r\n\r\n  \/\/ New improved type inference  \r\n  System.out.println(genericClass2.genericMethod(new GenericClass &lt; &gt; ()));\r\n }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nPeter\r\npeter\r\nJohn\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Type inference is a feature of Java which provides ability to compiler to look at each method invocation and corresponding declaration to determine the type of arguments. Java provides improved version of type inference in Java 8. Here, we are creating arraylist by mentioning integer type explicitly at both side. The following approach is used&hellip; <a href=\"https:\/\/codethataint.com\/blog\/what-is-type-inference\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[265],"tags":[],"class_list":["post-3244","post","type-post","status-publish","format-standard","hentry","category-concepts"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3244"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3244\/revisions"}],"predecessor-version":[{"id":3245,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3244\/revisions\/3245"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}