casting - Java cast: Which is better -- the imperative or programmatic way? -
i have seen there 2 ways cast object in java:
list<object> l = new arraylist(); arraylist<object> first = (arraylist<object>) l; arraylist<object> second = arraylist.class.cast(l);
which better , why?
the better method readable. if know class type cast, use (classtoupcast) object
. if don't know class but have class<classtoupcast> clazz
object, use clazz.cast(object)
.
Comments
Post a Comment