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

前端 未结 5 988
抹茶落季
抹茶落季 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:40

    Both of these statements are identical. Pick whichever one you find more readable. The second method is more common in my experience, and it is the once that I prefer.

    I tend to use the cast method solely when I am working with reflection, and it reads nicer in that situation. All other times I find myself using the second way of casting.

提交回复
热议问题