title: Java generic method type casting
body: <p>Why this happened? One line in the code works well while the other similar line doesn't. Does the automatic type cast only happen in certain conditions? I have tried to assign gt.echoV() to an Object and it works well; but when I assign it to a String, the same error will come out again.</p>
  
  <pre><code>public class GeneMethodTest {
  
      public static void main(String... args) {
          GeneMethodTest gt = new GeneMethodTest();
          gt.&lt;String&gt;echoV(); //this line works well
          gt.&lt;String&gt;echoV().getClass();//this line leads to a type cast exception                                                          
      }
  
      public &lt;T&gt; T echoV() {
          T t=(T)(new Object());                                                                    
          return t;
      }
  }
  </code></pre>