title: How is generic function implemented in java?
body: <p>As per my understanding the following generic function in java:</p>
  
  <pre><code>public static &lt;T&gt; T f(T x) {
     Integer[] arr = new Integer[4];
     T ret = (T) arr[2];
     return ret;
  }
  </code></pre>
  
  <p>is compiled to the following form (as it is unbounded):</p>
  
  <pre><code>public static Object f(Object x) {
     Integer[] arr = new Integer[4];
     Object ret = (Object) arr[2];
     return ret;
  }
  </code></pre>
  
  <p>However, when I run the following statement, the compiler is able to figure out the return value to be Integer type. How does the compiler figure it out?</p>
  
  <pre><code>Integer i = f(new Integer(4));
  </code></pre>
  
  <p>Shouldn't the function be written as following for the above statement to work?</p>
  
  <pre><code>  public static &lt;T extends Integer&gt; T f(T x) {
         Integer[] arr = new Integer[4];
         T ret = (T) arr[2];
         return ret;
      }
  </code></pre>