billc.cn
billc.cn

Reputation: 7317

The efficiency of Java Class.cast() compared to normal cast?

I am working on a generic class and I want to use Class.cast() to do some casting to avoid those nasty unchecked cast warnings or the @SupressWarning annotation on the methods.

I think Class.cast() should be basically the same thing as casting directly. But like all reflection methods, it might not be the case. Does anyone know the exact different between them?

Upvotes: 5

Views: 1265

Answers (1)

Puce
Puce

Reputation: 38132

I recommend using Class.cast() over @SuppressWarnings where possible.

Don't care about performance here. If you have performance issues, profile your application, and I would be very surprised if this turned out to be a hot spot.

Upvotes: 6

Related Questions