In Java, why is there no generic type info at run time?

后端 未结 3 1955
悲&欢浪女
悲&欢浪女 2020-12-16 05:10

Consider this example taken from a book, with a super class Gen and a subclass Gen2...

class Gen { }

class Gen2 extends Gen { }
<         


        
3条回答
  •  有刺的猬
    2020-12-16 05:49

    Generics were introduced to the Java language to provide tighter type checks at compile time and to support generic programming. [...] Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead.

    I don't think this is related to backwards compatibility at all. If you compile your code targetting 1.5 or more, the code will be not compatible with 1.4, regardless the use of generics.

提交回复
热议问题