| Author |
What is an unchecked cast and how do I go about fixing it?
|
Samuel Arwood
Greenhorn
Joined: Jul 08, 2012
Posts: 6
|
|
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[]
required: AnyType[]
array = (AnyType []) new Comparable[ newSize ];
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3065
|
|
Yeah, this is not gonna work.
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]; ?
|
 |
 |
|
|
subject: What is an unchecked cast and how do I go about fixing it?
|
|
|