Re: checking casts
- From: "Oliver Wong" <owong@xxxxxxxxxxxxxx>
- Date: Tue, 29 Nov 2005 21:46:34 GMT
"Thomas Hawtin" <usenet@xxxxxxxxxxxxxxxxx> wrote in message
news:438cc365$0$1471$ed2619ec@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Dan Upton wrote:
>> When trying to compile something I was working on today, I got this error
>> based on a performing a cast:
>>
>> PuzzleSolver.java:311: warning: [unchecked] unchecked cast
>> found : java.lang.Object
>> required: java.util.Vector<Piece>
>> piecesClone = (Vector<Piece>)pieces.clone();
>>
>> I know it's a safe cast, but just for the sake of getting javac to leave
>> me alone I tried sticking it in a try...catch block:
>
> The compiler is complaining that that you are casting to Vector<Piece>,
> but the runtime will not be able to check that the object isn't actually,
> say Vector<String>.
>
> The easy way around it is to write:
>
> piecesClone = new ArrayList<Piece>(pieces);
Alternatively, you could add:
@SuppressWarnings("unchecked")
to suppress the warning within a block of code (might be a good idea to
add a comment explaining why you believe it's safe to ignore this warning).
Sun's "JavaC" compiler is supposed to support the SuppressWarnings
annotation in its next release, and Eclipse's compiler already does support
it.
- Oliver
.
- Follow-Ups:
- Re: checking casts
- From: Thomas Hawtin
- Re: checking casts
- References:
- checking casts
- From: Dan Upton
- Re: checking casts
- From: Thomas Hawtin
- checking casts
- Prev by Date: abstract class and method
- Next by Date: Creating an object during runtime
- Previous by thread: Re: checking casts
- Next by thread: Re: checking casts
- Index(es):
Relevant Pages
|
|