When should I use the java 5 method cast of Class?

前端 未结 5 986
抹茶落季
抹茶落季 2020-12-11 02:22

Looking through some code I came across the following code

trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto));

and I\'d like

5条回答
  •  时光取名叫无心
    2020-12-11 02:30

    The main case for doing it (IME) is when you need to safely cast in a generic class/method. Due to type erasure, you can't cast to T but if you've been provided a Class parameter then you can use that to cast and the result will be assignable to a variable of type T.

提交回复
热议问题