title: Understanding Type Erasure in Java
body: <p>I have the following Java code that I tried out in JShell. </p>
  
  <pre><code>class X&lt;A&gt; {
    A id(A a) {
      return a;
    }
  }
  </code></pre>
  
  <p><strong>Case 1</strong></p>
  
  <pre><code>X&lt;Integer&gt; w = new X&lt;Integer&gt;();
  w.id(5)
  </code></pre>
  
  <p>In this case, JShell prints just 5, as I had expected. I expect the identity function in w to be parameterized with type <code>Integer</code> and thus expects nothing but a <code>Integer</code>. Supplying variables that are not subtypes of Integers causes this function to error.</p>
  
  <p><strong>Case 2</strong></p>
  
  <pre><code>X x = new X&lt;Integer&gt;();
  x.id(5)
  </code></pre>
  
  <p>JShell does output 5, but along with 5, it also outputs this error message:</p>
  
  <pre><code>|  Warning:
  |  unchecked call to id(A) as a member of the raw type X
  |  x.id(5)
  |  ^-----^
  </code></pre>
  
  <p>What does it mean by an unchecked call to id(A)? It doesn't seem to infer the type of <code>x</code> to be <code>X&lt;Integer&gt;</code> as I'm also able to run <code>x.id("5")</code> with just a warning which is isn't possible in case 1. Does this mean the identity function in <code>x</code> is polymorphic (with respect to the type of variable supplied)?</p>
  
  <p><strong>Case 3</strong></p>
  
  <pre><code>X y = new X&lt;&gt;()
  y.id(5)
  X z = new X()
  z.id(5)
  </code></pre>
  
  <p>This situation is identical to case 2. However, I am unable to wrap my mind around the code. What is the parameterized type of y? Are objects y and z identical apart from the fact that they are two separate objects?</p>
  
  <p>I'm guessing the notion of type erasure is playing a part in this but am unable to truly understand it and explain the phenomenon above.</p>