checking casts

Discussion in 'Java' started by Dan Upton, Nov 29, 2005.

  1. Dan Upton

    Dan Upton Guest

    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?
     
    Dan Upton, Nov 29, 2005
    #1
    1. Advertising

  2. 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);

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Nov 29, 2005
    #2
    1. Advertising

  3. Dan Upton

    Oliver Wong Guest

    "Thomas Hawtin" <> wrote in message
    news:438cc365$0$1471$...
    > 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
     
    Oliver Wong, Nov 29, 2005
    #3
  4. Oliver Wong wrote:
    >
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Nov 29, 2005
    #4
  5. Dan Upton

    Chris Smith Guest

    Thomas Hawtin <> wrote:
    > Oliver Wong wrote:
    > >
    > > 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.


    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.

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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Dec 1, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2hyaXMgRGF2b2xp?=

    Web casts in ASP.Net

    =?Utf-8?B?Q2hyaXMgRGF2b2xp?=, Oct 19, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    512
    clintonG
    Oct 19, 2005
  2. cgbusch
    Replies:
    2
    Views:
    331
    Sudsy
    Jul 8, 2003
  3. Joona I Palaste

    Needless casts?

    Joona I Palaste, Apr 24, 2004, in forum: Java
    Replies:
    15
    Views:
    694
    Icemerth
    Apr 25, 2004
  4. Wenjie

    C++ casts on zero

    Wenjie, Aug 17, 2003, in forum: C++
    Replies:
    11
    Views:
    674
    Wenjie
    Aug 24, 2003
  5. Jan Engelhardt

    Type-checking casts for GNU C

    Jan Engelhardt, Dec 23, 2008, in forum: C Programming
    Replies:
    34
    Views:
    1,296
    David Thompson
    Jan 5, 2009
Loading...

Share This Page