Are generics removed by the compiler at compile time

后端 未结 4 740
陌清茗
陌清茗 2020-12-17 21:19

In this tutorial on reflection it states:

[...] because generics are implemented via type erasure which removes all information regarding generic typ

4条回答
  •  心在旅途
    2020-12-17 21:29

    In java, Generics is just a place holder. Java Run Time doesn't have any clue about the generics. It's all a compile time trick.

    Setting up generics is exactly similar to the scenario, when you declare a field attribute in a class as

    • Object (When you just declare type as T)
    • MyObject (When you declare type as T extends MyObject).

    After compilation, all would be wired according to the types. That's what is known as Type Erasure.

提交回复
热议问题