<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Deepayan Ghosh on Medium]]></title>
        <description><![CDATA[Stories by Deepayan Ghosh on Medium]]></description>
        <link>https://medium.com/@deepayanghosh?source=rss-3284ed26ec0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*f5F0HrWBduILH7NP.</url>
            <title>Stories by Deepayan Ghosh on Medium</title>
            <link>https://medium.com/@deepayanghosh?source=rss-3284ed26ec0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 17:26:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@deepayanghosh/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[“Bounded” yet “wild” — Java Wildcards]]></title>
            <link>https://medium.com/@deepayanghosh/bounded-yet-wild-java-wildcards-718141e561d1?source=rss-3284ed26ec0------2</link>
            <guid isPermaLink="false">https://medium.com/p/718141e561d1</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[generics]]></category>
            <dc:creator><![CDATA[Deepayan Ghosh]]></dc:creator>
            <pubDate>Sat, 11 Aug 2018 15:42:40 GMT</pubDate>
            <atom:updated>2018-08-11T15:42:40.251Z</atom:updated>
            <content:encoded><![CDATA[<p>Today during J2EE training, I came across this construct— List&lt;?&gt;. While exploring various resources to understand it, I came across two more: List&lt;?extends T&gt; and List&lt;? super T&gt; . They are often misinterpreted and as a result their behavior is often labeled “weird”. I found that many people are confused about these constructs, so this article aims to give a better understanding of the above.</p><h4>So, what misinterpretation am I talking about?</h4><p>Let’s say we have: List&lt;? extends Number&gt; list. Now some of you may interpret this as “list points to a generic container which contains elements of type Number or any of its sub-classes”. As a result, when you try to do this:</p><pre>List&lt;? extends Number&gt; list = new ArrayList&lt;Integer&gt;();<br>list.add(123);</pre><p>and are greeted with a compilation error, you become confused as to why you cannot add a value to the list. Here, &lt;? extends Number&gt; is interpreted as defining the kind of elements in the list. Sadly, this is a wrong notion.</p><h4>What is wildcard?</h4><p>Wildcard is basically the<strong> “?” </strong>symbol. It represents the unknown type.</p><p>Lets say we have a generic class Compare&lt;T&gt; which is a wrapper around an array of typeT .</p><pre>class Compare&lt;T&gt; {<br>    T[] arr;<br>    Compare(T[] val) {<br>        this.arr = val;<br>    }<br>    public boolean compare(Compare&lt;?&gt; second) {<br>        //body<br>    }<br>}</pre><p>It has a method compare() whose purpose is to compare two objects of Compare. So we define compare() as: public boolean compare(Compare&lt;?&gt; second) . Here &lt;?&gt; means that the parameter second can accept objects of Compare&lt;T&gt; where can be any type, i.e, it can accept Compare&lt;Object&gt;, Compare&lt;Integer&gt;, Compare&lt;Double&gt;, Compare&lt;Integer&gt; etc. But this presents a problem. What if the invoking object is Compare&lt;Integer&gt; and second accepts argument of type Compare&lt;String&gt; . We can’t compare integer with strings. So, we need to restrict second to accept objects whose type-parameter is “Numeric”, that is, restrict it to accept arguments whose type-parameter descends from class Number.</p><h4>&lt;? extends T&gt;</h4><p>The above problem is solved by &lt;? extends T&gt;. If in compare(), we specify parameter second as Compare&lt;? extends Number&gt; second then second can accept Compare objects whose type-argument is Number or any of its sub-classes. In general, if the type of a variable is Compare&lt;? extends T&gt; it means that “the variable can refer to objects of Compare whose type-argument is T or sub-classes of T”. So, Compare&lt;? extends Number&gt; second can refer to objects of class Compare whose type-argument is Number of any sub-class of Number like Compare&lt;Integer&gt;, Compare&lt;Double&gt; but not Compare&lt;String&gt;.This is called <strong>“upper-bounded” </strong>wildcards, allowing inheritance to spread downwards but not upwards.</p><h4>&lt;? super T&gt;</h4><p>Just as &lt;? extends T&gt; allows any sub-classes of T, &lt;? super T&gt; allows all super-classes of T. That is, if the type-parameter of a variable is &lt;?super T&gt; , then the variable can refer to objects whose type-argument is T or super-class of T .So, Compare&lt;? super Integer&gt; second means that second can refer to Compare objects whose type-argument is Integer or super-classes of Integer . This is called <strong>“lower-bounded” </strong>wildcards since flow of inheritance is restricted downwards but can spread-out upwards.</p><blockquote>The point is: &lt;? extends T&gt; or &lt;? super T&gt; defines the family of values that a variable can refer to or point to. The values, that can be added or read, depends on the actual object that the variable is pointing to.</blockquote><h3>List &lt;? extends T&gt; list</h3><p>Here list can point to any member of the family of List whose type-argument is T or any sub-class of T . We will assume T to be the class Number from here.</p><pre>List&lt;? extends Number&gt; list = new ArrayList&lt;Number&gt;();<br>List&lt;? extends Number&gt; list = new ArrayList&lt;Integer&gt;();<br>List&lt;? extends Number&gt; list = new ArrayList&lt;Double&gt;();</pre><p>All the above are legal assignments because Integer, Double are sub-classes of Number, but the one below is not since String is not a sub-class of Number:</p><pre>List&lt;? extends Number&gt; list = new ArrayList&lt;String&gt;(); </pre><p>In the above assignments, list does not exactly know what type the assigned container is holding, but it knows this much that the “type” extends from Number. This is the only thing that it actually cares about. As long as the assigned container has type Number or any sub-class of Number , it is happy.</p><h4>Implications</h4><p>Since list does not know which exact type of value the underlying container is holding, <strong>you cannot add elements to it</strong>. Even if you assign list = new ArrayList&lt;Integer&gt;() you cannot add 1 to list because list cannot guarantee that the underlying ArrayList has type Integer , it only knows that the assigned ArrayList has a type which extends from Number and there are several sub-classes of Number. It can be be pointing to an ArrayList of Double and you may try to add an Integer , or vice versa. Even though Integer and Double are sub-classes of Number, they cannot exist in the same ArrayList of type Integer together. Since it cannot determine what exact sub-class of Number the underlying container is holding, it does not allow you to add values to it to maintain type safety.</p><blockquote><strong>You</strong>: Hey list add “1.342” to your container.<br><strong>list </strong>: I cannot do that I only hold some kind of numbers.<br><strong>You </strong>: Okay, then add 1.342.<br><strong>list </strong>: I cannot do that. I am not sure whether I am holding Doubles. I may be holding Integers, so I cannot add a Double, it would compromise type-safety.</blockquote><p>However, you <strong>can read</strong> from such a list because while reading, it can guarantee that the values coming out are sub-classes of Number. The value coming out, Integer or Double or anything, are all sub-classes of Number. The return type is known here, because of which reading is allowed.</p><blockquote><strong>You cannot add to a list whose type is </strong><strong>&lt;? extends T&gt; but can read from it.</strong></blockquote><h4>List &lt;? super T&gt; list</h4><p>It is just the opposite of the above. A variable with the type List&lt;? super T&gt; means that the variable can point to any list which contains values of type T or super-class of T. Considering T to be Number all following assignments are legal:</p><pre>List&lt;? super Number&gt; list= new ArrayList&lt;Number&gt;();<br>List&lt;? super Number&gt; list= new ArrayList&lt;Object&gt;();</pre><p>They are legal because Number is of the same type as T (Number) and Object is a super-class of T(Number) . However just like previous case, it does not know whether the underlying list contains Numbers or Objects , it only knows that whatever is inside the container, they are either Number or super-classes of Number.</p><h4>Implications</h4><p>Now here also people think that &lt;? super T&gt; defines the elements in list and try to add Object to list which results in compilation error because actually,list cannot not guarantee what type of elements the underlying list contains, it only knows that it can refer to lists whose elements can come from a family of super-classes of T(Number). The underlying list may hold Numbers and you may try to add Objects which will compromise type-safety. So to maintain type-safety it does notallow addition of values which are super-classes of Number because list cannot guarantee what values underlying container contains.</p><p>However you can add elements of type T or sub-classes of T to such a list. In this case there is a guarantee that the underlying list (whatever it may hold) has a type which serve as a super-class to the element being inserted. For example, you can always add an Integer to an ArrayList of Object or Number.</p><blockquote>You may again get confused that here we can add sub-classes to list but in &lt;? extends T&gt; we cannot. Remember it is always about the type of container that list can refer to. <br>In case of &lt;? extends T&gt; the underlying list contains objects of type T or sub-class of T . For example, it can be an ArrayList of Integers or Doubles specifically. Just as you cannot add a Double to an ArrayList of Integers. the underlying list of list may be ArrayList of Integers and you may try to add a Double value. list itself cannot determine exactly what type of values the container holds. This also holds for &lt;? super T&gt; . It does not allow addition of values of type T or super-class of T because it cannot determine what the list it refers to actually contains.<br>However you can add T or sub-classes of T to &lt;? super T&gt; because super-class lists can always accommodate values of sub-class type. For example, an ArrayList of type Object definitely contain value of type Integer. Similarly if T is Number, the underlying list may hold Object or Number, but it can always accommodate Integers,Doubles and so on.</blockquote><p>You can read from from a list with &lt;? super t&gt; but you will always get an Object because it cannot determine which specific type of value the underlying list contains. In this case, the values can be anything among Tor any of its super-classes. Since Object is the super-class of all kinds of values, you will always get back an Object.</p><blockquote><strong>You can add to a list of type &lt;? super T&gt; but the element being added must be </strong><strong>T or a sub-class of </strong><strong>T. You can also read objects from such a list but you will always get back an </strong><strong>Object.</strong></blockquote><h3>List&lt;?&gt; list</h3><p>The above translates to List&lt;? extends Object&gt; list. It can refer to or point to any kind of list since everything is a sub-class of Object .</p><p>So you cannot add anything to such a list but can read from it, it will always return an Object.</p><h3>Conclusion</h3><ol><li>If you want a list which can guarantee that you will always get back values of type T use &lt;? extends T&gt;. You will never be able to add to such lists.</li><li>If you want a list which can guarantee that you will be able to add T or sub-classes of T use &lt;? super T&gt; . You may read from such lists but will always get back an Object.</li></ol><p>In case of parameters,</p><ol><li>If you want a list variable which can accept lists containing elements of type T or sub-classes of T , declare the variable as &lt;? extends T&gt;. Remember such a list cannot be modified.</li><li>If you want a list variable which which can accept lists containing elements of type T or super-classes of T , declare the variable as &lt;? super T&gt;. Remember reading from such a list always gives Object.</li><li>If you are not sure about T that is the type of lists that may get passed or returned, use &lt;?&gt;.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=718141e561d1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>