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

G

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
 
K

klynn47

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.
 
R

Ryan Stewart

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.
 
G

Guest

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top