{"id":9,"date":"2008-01-27T20:09:11","date_gmt":"2008-01-28T00:09:11","guid":{"rendered":"http:\/\/joemorrison.org\/blog\/2008\/01\/27\/understanding-java5-generics\/"},"modified":"2008-01-27T20:09:57","modified_gmt":"2008-01-28T00:09:57","slug":"understanding-java5-generics","status":"publish","type":"post","link":"https:\/\/morrison.today\/blog\/2008\/01\/27\/understanding-java5-generics\/","title":{"rendered":"Understanding Java5 Generics"},"content":{"rendered":"<p>Good article on Java5 Generics by Martin Wolf:<\/p>\n<p><a href=\"http:\/\/blogs.infosupport.com\/martinw\/articles\/generics.aspx\" title=\"Advanced Java5 Generics\">http:\/\/blogs.infosupport.com\/martinw\/articles\/generics.aspx<\/a><\/p>\n<p>It seems many programmers are confused about generics, in particular the use of the <code>? extends ...<\/code> notation. The question mark is called a <em>type wildcard<\/em>, and is typically used as the value of a type parameter in a generic method. It means that wherever the method is invoked in your code, the compiler infers a specific type to be substituted for the wildcard and enforces that at compile time. The notation <code>? extends X<\/code> is a <em>bounded wildcard<\/em>, meaning that the deduced type must be a subtype of <code>X<\/code>.  Here&#8217;s a variation on Wolf&#8217;s example which I think might help clarify the difference.<br \/>\n<!--more--><br \/>\nSay you have a class <code>Animal<\/code> which supports the method <code>feed()<\/code>, with subclasses <code>Cat<\/code> and <code>Dog<\/code>. Consider this code which does not use generics:<\/p>\n<pre>\r\npublic static void feedAnimals1(List&lt;Animal&gt; pets) {\r\n\r\n\tfor (Animal a : pets) {\r\n\t\ta.feed();\r\n\t}\r\n\r\n\tfor (int i=0; i &lt; pets.size(); i++) {\r\n\t\tpets.set(i, (i % 2 == 0) ? new Cat() : new Dog());\r\n\t}\r\n}<\/pre>\n<p>The argument <code>pets<\/code> is of type <code>List&lt;Animal&gt;<\/code>. That means that each element can be a <code>Cat<\/code> or a <code>Dog<\/code> object. They all understand the <code>feed()<\/code> method, and any element can be reassigned to a <code>Cat<\/code> or <code>Dog<\/code> object (since the elements are of type <code>Animal<\/code>).<\/p>\n<p>But what if we write it this way instead?<\/p>\n<pre>\r\npublic static void feedAnimals2(List&lt;? extends Animal&gt; pets) {\r\n\r\n\tfor (Animal a : pets) {\r\n\t\ta.feed();\r\n\t}\r\n\r\n\tfor (int i=0; i &lt; pets.size(); i++) {\r\n\t\tpets.set(i, (i % 2 == 0) ? new Cat() : new Dog());    \/\/ compile error\r\n\t}\r\n}<\/pre>\n<p>This second version uses generics. In this case we are <em>not<\/em> saying that the input must be of type <code>List&lt;Animal&gt;<\/code>. Rather, we are saying that the input must be of type <code>List&lt;X&gt;<\/code> where X is any subtype of Animal. At each location in the code where this method is called, the Java compiler will guess a suitable type for X at compile time and enforce it. It&#8217;s exactly as though you had written different overloaded versions of this method, one for lists of Cat objects, one for lists of Dog objects, and so on.<\/p>\n<p>In the generic example the compiler will not let you randomly assign dogs and cats to the list elements, because the caller might legitimately pass in a list of Dog objects in which case the cat assignment would be illegal, and vice versa. But apart from that restriction, the generic version is more flexible. It can work on inputs of type <code>List&lt;Animal&gt;<\/code>, <code>List&lt;Dog&gt;<\/code>, or <code>List&lt;Cat&gt;<\/code> e.g.<\/p>\n<pre>\r\n\tList&lt;Dog&gt; pets = new ArrayList&lt;Dog&gt;();\r\n\tpets.add(new Dog());\r\n\tpets.add(new Dog());\r\n\tfeedAnimals1 (pets);    \/\/ compile error - pets must be of type List&lt;Animal&gt;\r\n\tfeedAnimals2 (pets);    \/\/ ok<\/pre>\n<p>The Sun tutorial on generics explains all in detail:<\/p>\n<p><a href=\"http:\/\/java.sun.com\/j2se\/1.5\/pdf\/generics-tutorial.pdf\">http:\/\/java.sun.com\/j2se\/1.5\/pdf\/generics-tutorial.pdf<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Good article on Java5 Generics by Martin Wolf: http:\/\/blogs.infosupport.com\/martinw\/articles\/generics.aspx It seems many programmers are confused about generics, in particular the use of the ? extends &#8230; notation. The question mark is called a type wildcard, and is typically used as the value of a type parameter in a generic method. It means that wherever the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,7],"tags":[],"class_list":["post-9","post","type-post","status-publish","format-standard","hentry","category-general-development","category-java"],"blocksy_meta":"","_links":{"self":[{"href":"https:\/\/morrison.today\/blog\/wp-json\/wp\/v2\/posts\/9","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/morrison.today\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/morrison.today\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/morrison.today\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/morrison.today\/blog\/wp-json\/wp\/v2\/comments?post=9"}],"version-history":[{"count":0,"href":"https:\/\/morrison.today\/blog\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"wp:attachment":[{"href":"https:\/\/morrison.today\/blog\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/morrison.today\/blog\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/morrison.today\/blog\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}