What are the risks of explicitly casting from a list of type List<? extends MyObject> to a list of type List in Java?

后端 未结 5 1199
说谎
说谎 2021-01-12 16:26

I think the title should explain it all but just in case...

I want to know what risks and potential issues relating to casting can arise from the following snippet o

5条回答
  •  感动是毒
    2021-01-12 16:29

    In addition to the answers already posted, take a look at the following

    • what-is-the-meaning-of-the-type-safety-warning-in-certain-java-generics-casts
    • confused-by-java-generics-requiring-a-cast

    The compiler generates a warning incase an element from the type is accessed later in your code and that is not the generic type defined previously. Also the type information at runtime is not available.

提交回复
热议问题