Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

checking casts

Thread view: 
Dan Upton - 29 Nov 2005 20:51 GMT
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:

Vector<Piece> piecesClone=null;
try{
   piecesClone = (Vector<Piece>)pieces.clone();
}catch(ClassCastException cce){
   System.out.println("This is just so javac will shut up");
}

I still get the warning from the compiler though, so I'm assuming this
*isn't* the correct way to check a cast.  Any tips on what *is* then?
Thomas Hawtin - 29 Nov 2005 21:13 GMT
> When trying to compile something I was working on today, I got this
> error based on a performing a cast:
[quoted text clipped - 6 lines]
> 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);

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Oliver Wong - 29 Nov 2005 21:46 GMT
>> When trying to compile something I was working on today, I got this error
>> based on a performing a cast:
[quoted text clipped - 14 lines]
>
>         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
Thomas Hawtin - 29 Nov 2005 23:33 GMT
>     Alternatively, you could add:
>
> @SuppressWarnings("unchecked")

Perhaps it was a good idea that javac doesn't initially support
SuppressWarnings. Hopefully it should have encouraged programmers to
adopt idioms that don't cause warnings, rather than treating the symptoms.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Chris Smith - 01 Dec 2005 18:20 GMT
> >     Alternatively, you could add:
> >
[quoted text clipped - 3 lines]
> SuppressWarnings. Hopefully it should have encouraged programmers to
> adopt idioms that don't cause warnings, rather than treating the symptoms.

The problem with this is that there are things that can't be done in
Java 1.5 without warnings.  Your code above sorta lucked out.  If the
method had returned List instead of Vector and the new List needed to
have similar performance characteristics to the old one, then it would
definitely not have been good enough.

This is certainly not a clear-cut case of right or wrong.  Certainly
it's best to avoid SuppressWarnings when possible, but it's equally good
to be aware of its presence.

It's unfortunate, though, that SuppressWarnings apparently can't be
applied to a block or statement; only to an API element, parameter,
local variable.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.