title: Java Generics List
body: <p>Following is an example of Generics with wild card</p>
  
  <pre><code>public static void printListItems(List&lt;object&gt; list) {
          for (Object listItem : list)
              System.out.println(listItem);
      }
  </code></pre>
  
  <p>In this example we want to print list items of any type but it can’t print <code>List&lt;Integer&gt;</code>, <code>List&lt;String&gt;</code> etc. because they are not subtypes of <code>List&lt;Object&gt;</code>. This problem can be solved using unbounded wildcard.</p>
  
  <pre><code>public static void printListItems(List&lt;?&gt; list) {
      for (Object listItem : list)
          System.out.println(listItem);
  }
  </code></pre>
  
  <p>I read this above code in Java tutorial. For the first example , it says it cannot work because <code>List&lt;String&gt;</code> is not sublass of <code>List&lt;Object&gt;</code>.</p>
  
  <p>Then why it is so that in the second example the for loop is working with taking listItem as dataType of <code>Object</code> and iterating through <code>List&lt;String&gt;</code> elements.</p>