Why “t instanceof T” is not allowed where T is a type parameter and t is a variable?

后端 未结 7 1738
Happy的楠姐
Happy的楠姐 2021-01-05 11:03

Eclipse says that the instanceof operation is not allowed with Type Parameter due to generic type eraser.

I agree that at runtime, no type information stays. But con

7条回答
  •  [愿得一人]
    2021-01-05 11:33

    But if I instantiate this class of type Integer, then the corresponding object will have a field t of type Integer.

    Actually, it wouldn't. It'd have a field t of type Object. As you've said, generics are almost entirely syntactic sugar (the exception is that when you extend a generic class and specify a type parameter, the type remains as metadata in the class file).

提交回复
热议问题