Re: = operator should automatically perform appropriate casts

Discussion in 'Java' started by cgbusch, Jul 7, 2003.

  1. cgbusch

    cgbusch Guest

    >
    > As for automatic downcasting in an assignment statement, I think
    > it's a very bad idea.

    <deleted>
    > A fundamental design goal of the Java language is to find as
    > many potential coding errors at compile time as possible, rather
    > than at runtime. That's goodness.


    The compiler would still check for 'inconvertible types', since the
    object on the left must be able to be a subclass or superclass of the
    object on the right.

    List list=new ArrayList();
    list.add("oops");
    Map m=(Map)list.get(0);

    What Java compiler will find a bug in the above code? They won't. So
    (Map) is useless additional information since we know it will need to
    be a Map assigned anyways, so (Map) for all practical purposes you are
    trusting the compiler's arm.

    Map m=(implicit)list.get(0) would accomplish the same thing and it
    stills check for 'inconvertible types' at compile-time.

    --
    CB
     
    cgbusch, Jul 7, 2003
    #1
    1. Advertising

  2. cgbusch

    cgbusch Guest

    > > Map m=(Map)list.get(0);
    > Right now compiler complains here because it tries to tell you that you're
    > trying to do something potentially dangerous and wants you to make clear
    > that you *really* want to do it :)

    Then I could do it via (implicit), like so:
    Map m=(implicit)list.get(0);
    But if it was so potentially dangerous, then why are we forced to do it so often?
     
    cgbusch, Jul 8, 2003
    #2
    1. Advertising

  3. cgbusch

    Sudsy Guest

    cgbusch wrote:
    >>>Map m=(Map)list.get(0);

    >>
    >>Right now compiler complains here because it tries to tell you that you're
    >>trying to do something potentially dangerous and wants you to make clear
    >>that you *really* want to do it :)

    >
    > Then I could do it via (implicit), like so:
    > Map m=(implicit)list.get(0);
    > But if it was so potentially dangerous, then why are we forced to do it so often?


    Because a lot of these methods are defined as returning Object in order
    to be as general as possible. It's up to the programmer to know what
    kind of objects got stuffed into the collection and cast accordingly.
    The JVM will very helpfully throw a runtime ClassCastException if you
    get it wrong. Then again, you could always use instanceof in order to
    determine whether what you were expecting is what you actually have.
     
    Sudsy, Jul 8, 2003
    #3
    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. CoX
    Replies:
    3
    Views:
    494
  2. Aditi Deo via .NET 247
    Replies:
    0
    Views:
    375
    Aditi Deo via .NET 247
    Jun 5, 2004
  3. Ehsan Khoddam mohammadi

    unsigned right shift casts to int automatically!!!

    Ehsan Khoddam mohammadi, Jan 4, 2007, in forum: Java
    Replies:
    2
    Views:
    429
    Ehsan Khoddam mohammadi
    Jan 4, 2007
  4. Roedy Green
    Replies:
    5
    Views:
    467
    Tom Anderson
    Aug 25, 2008
  5. Chicken McNuggets
    Replies:
    19
    Views:
    464
    88888 Dihedral
    Jan 28, 2013
Loading...

Share This Page