title: Java - Generic types and collections
body: <p>I'm trying to learn about the use of generic types and I've noticed something weird when I was experimenting with some lines of code.</p>
  <p>The first piece of code is inside a class named &quot;A&quot;:</p>
  <pre><code>public void func(int k, List list) {
      list.add(9);
      list.add(true);
      list.add(&quot;a string&quot;);
      }
  </code></pre>
  <p>The second piece of code is in a different class, inside the main function:</p>
  <pre><code>List&lt;Integer&gt; arr = new ArrayList&lt;Integer&gt;();
          arr.add(14);
          System.out.println(arr.toString());
          a.func(8, arr);
          System.out.println(arr.toString());
  </code></pre>
  <p>Running the code results in this lines being printed:</p>
  <blockquote>
  <p>[14]</p>
  <p>[14, 9, true, a string]</p>
  </blockquote>
  <p>This got me pretty confused since <code>arr</code> is an <code>ArrayList</code> of type <code>Integer</code>, how can it contain objects of type <code>boolean</code> and <code>String</code>? Is there a transformation of the list in the function <code>func</code> to a raw type (which mean it becomes of generic type <code>Object</code>)? And if so how is it possible since you cannot do this for example: <code>List&lt;Integer&gt; arr = new ArrayList&lt;Object&gt;();</code>?</p>
  <p>Would love some clarification on this, maybe it will help me grasp this subject of generic types better. Thanks!</p>