• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object array initialization  RSS feed

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

these lines compile fine :



But these do not compile :



As far as I know Object[] IS-A Object. So why I am getting this compilation error : Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from Object[] to Object
?

Similiar problem :

this line compiles :


but these line don't compile (same compilation error as before):


Please, can someone explain this to me?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exact details will be in the Java Language Specification (I couldn't find the exact place in a quick search), but basically the {..} (without a preceding array type) is a shorthand expression. And it's only allowed when the compiler can work out what sort of array to create.
In 1, it's been assigned to an Object[] variable, so the compiler can tell it should create an Object[]. In 2, the same applies to the outer array, and the inner array is fullt specified.

In 3 and 4, the outer array is fine, but the compiler doesn't know what to do with the inner array. In 5, the outer array is assigned to a plain Object variable, so again, the compiler doesn't know what to create.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!