What is an unchecked cast and how do I go about fixing it?
posted 3 years ago
Hi everyone. I'm back again, just now with this unchecked casting issue. I have never seen it before and all the google searches turn up code that is way beyond me. I'll post a snippet of the code giving me the problem and then the error the compiler gives me. Any help would be greatly appreciated. Thanks in advance.
and during compiling i get this message:
BinaryHeap.java:76: warning: [unchecked] unchecked cast
found : java.lang.Comparable
array = (AnyType ) new Comparable[ newSize ];
You're using a raw type in your code, which is giving you the unchecked cast problem. You normally fix this by using a fully parameterized class instead: Comparable<Whatever>. In this case it's not gonna work though, because you can't create an array of a generic type. Comparable<Whatever> can not be instantiated.
Anyway, I'm not really getting what the point of casting an array of Comparable to an array of AnyType is. Why not just do array = new AnyType[newSize]; ?
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.