Results 1 to 9 of 9
  1. #1
    guest_user is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default downcasting in java

    when i try to cast a reference to an object to an arraylist of strings, i get a warning of unchecked casting, how do I remove it?

    I used something like (ArrayList<String>)obj;

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Instead of "I used something like" why don't you post what you actually used? A SSCCE will provide us with much more information about your problem.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by guest_user View Post
    when i try to cast a reference to an object to an arraylist of strings, i get a warning of unchecked casting, how do I remove it?

    I used something like (ArrayList<String>)obj;
    You can't remove that warning; generics is a compile time only thing, i.e. during runtime only ArrayLists exist. If you take away the type parameter:

    Java Code:
    ArrayList al= (ArrayList)obj;
    ... the compiler will whine about your program using 'raw types'. You won't get a warning when you apply an ordinary down cast, as in:

    Java Code:
    Double d= (Double)obj;
    Here the down cast will be applied at runtime and if the cast fails an Exception will be thrown. Generic types always cause a warning here and the compiler assumes that you know what you're doing but it whines a little about it to make sure.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    guest_user is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    What about arrays, can I down cast an Object into a String array [] without having the compiler whine or warn?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by guest_user View Post
    What about arrays, can I down cast an Object into a String array [] without having the compiler whine or warn?
    Sure you can, an array (of whatever type) is just an extension of the Object class so a down cast (done at runtime) is no problem and the compiler leaves it all to the Java runtime.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by JosAH View Post
    You can't remove that warning
    Uhm, what about the SuppressWarnings annotation? ;-)

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by masijade View Post
    Uhm, what about the SuppressWarnings annotation? ;-)
    That´s smuggling; minus one point for Masijade.

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    guest_user is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Uhm, what about the SuppressWarnings annotation? ;-)
    sorry masijade but I'm no smuggler

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by guest_user View Post
    sorry masijade but I'm no smuggler
    Good; muffling away warnings fires back one day. The compiler doesn't issue a warning for no reason, no matter how futile it may seem. One day the unsafe situation you have created might stab you in the back while the compiler had warned you about it. If you really know (or you think you know) what you're doing silencing individual warnings can be forgiven but always be very careful.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •