The moose likes Beginning Java and the fly likes What is an unchecked cast and how do I go about fixing it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is an unchecked cast and how do I go about fixing it?" Watch "What is an unchecked cast and how do I go about fixing it?" New topic
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: 3989
    
  18

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]; ?


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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What is an unchecked cast and how do I go about fixing it?
 
It's not a secret anymore!