wondering why my array of strings (String[]) is not upcastable to Object ? _clb_

Discussion in 'Java' started by spambin@perigon.com, Dec 28, 2004.

  1. Guest

    Hi !


    I wonder if one of you nice java experts could help me with a real
    quick question.

    I am unable to get the code in 'FAILS TO COMPILE' (in the function
    below to work... well actually i have simple workaround... But this
    really started to annoy me. There's probably some subtle Java point
    i'm missing (or worse... maybe there's an eclipse bug?).
    anyway... if someone could tell me what the root of the problem might
    be, I'd be most grateful.

    CODE:

    void bizzare()
    {
    String[] sfoo = { "boo", "zoo"};
    Object o = sfoo; // String[] can be assigned to an Object

    final Object[][] good = {
    {"foo", sfoo },
    {"bar", sfoo }

    };

    // FAILS TO COMPILE.. not sure why
    // final Object[][] expected = {
    // {"foo", (Object){ "boo", "zoo"} },// Casting fails
    // {"bar", { "boo", "zoo"} } // so does not casting.
    // };
    //
    }


    FINALLY... my work-around:

    void workAround()
    {
    final Object[][] good = {
    {"foo", new String[] { "boo", "zoo"} },
    {"bar", new String[] { "boo", "zoo"} }

    };

    }


    thanks in advance for any advice / help you can spare....
    / chris
    , Dec 28, 2004
    #1
    1. Advertising

  2. Guest

    Re: wondering why my array of strings (String[]) is not upcastable to Object ? _clb_

    Is the syntax error you are getting an indication that { was expected
    and then that there was an illegal initializer for Object. I think the
    problem lies in the use of an initializer list. If a list creates a
    String array, it is automatically an Object. I can't find a specific
    statement about this, but in my experience the only time I'm able to
    use an initializer list is the first time I create a reference to a new
    array.
    , Dec 28, 2004
    #2
    1. Advertising

  3. Guest

    Re: wondering why my array of strings (String[]) is not upcastable to Object ? _clb_

    You might also consider creating an anonymous array

    new String[]{....}
    , Dec 28, 2004
    #3
  4. Ryan Stewart Guest

    <> wrote in message
    news:...
    > Hi !
    >
    > I wonder if one of you nice java experts could help me with a real
    > quick question.
    >
    > I am unable to get the code in 'FAILS TO COMPILE' (in the function
    > below to work... well actually i have simple workaround... But this
    > really started to annoy me. There's probably some subtle Java point
    > i'm missing (or worse... maybe there's an eclipse bug?).
    > anyway... if someone could tell me what the root of the problem might
    > be, I'd be most grateful.
    >
    > CODE:
    >
    > void bizzare()
    > {
    > String[] sfoo = { "boo", "zoo"};
    > Object o = sfoo; // String[] can be assigned to an Object
    >
    > final Object[][] good = {
    > {"foo", sfoo },
    > {"bar", sfoo }
    >
    > };
    >
    > // FAILS TO COMPILE.. not sure why
    > // final Object[][] expected = {
    > // {"foo", (Object){ "boo", "zoo"} },// Casting fails
    > // {"bar", { "boo", "zoo"} } // so does not casting.
    > // };
    > //
    > }
    >

    Nothing very bizarre about it. Would you expect the following to work?
    Object o = {"a", "b"};

    If so, why? If not, then you are correct, and you have your answer. In the
    code that doesn't compile, you're declaring an array of Object arrays? In
    the initializer, you have two open braces followed by "foo". That means the
    Object in the first position of the first object array (i.e. expected[0][0])
    is the String "foo" which is an Object, so that works. Then you attempt to
    place the following into expected[0][1]: {"boo", "zoo"}. However, the type
    of expected[0][1] is Object, and {"boo", "zoo"} is not a valid initializer
    for Object, which might be what your compiler was telling you. You may
    *only* use the special array initializer when initializing an explicitly
    declared array. The fact that an array is an Object is irrelevant.
    Ryan Stewart, Dec 28, 2004
    #4
  5. Guest

    Re: wondering why my array of strings (String[]) is not upcastable to Object ? _clb_

    Thank you, Ryan. that makes sense. My understanding of what you
    said is that array initializers of
    the form { item, item, item } are only meant to be used when
    initializing an array.. not in other contexts (like
    i was trying to use them). Works for me (all the more so since i
    have a work-around)

    appreciate your response.
    /chris
    , Dec 28, 2004
    #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. David Zimmerman
    Replies:
    1
    Views:
    335
    Chris Bedford
    Aug 15, 2003
  2. Chris Bedford
    Replies:
    0
    Views:
    568
    Chris Bedford
    Aug 21, 2003
  3. Chris Bedford
    Replies:
    1
    Views:
    664
    Ben Edgington
    May 21, 2004
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,763
    Smokey Grindel
    Dec 2, 2006
  5. find clausen

    wondering why ...

    find clausen, Feb 11, 2007, in forum: Javascript
    Replies:
    2
    Views:
    61
    find clausen
    Feb 11, 2007
Loading...

Share This Page