Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1207 :
Posted: Tue Jan 20, 2015 10:47 pm
by coder007
Second option
Code: Select all
new Object[]{ "aaa", new Object(), new ArrayList(), {} };
you did say:
{ } doesn't define any kind of Object.
But why this is legal when we initialize 2-dimensional array?
Code: Select all
Object arr[][] = new Object[][] {new String[], {} };
Re: About Question enthuware.ocajp.i.v7.2.1207 :
Posted: Tue Jan 20, 2015 11:03 pm
by admin
{ } in your case is part of a syntax for an array definition. You can also do something like String[] sa = { }; and this would define a String[] object. That is just something that the language designers have allowed in the language.
But it is true that { } on its own means nothing at the least and creating a new scope at the most. It doesn't define any object as such. You cannot just write { } and expect to have an object (which is what the code in the given option expects.)
On a second thought, I think you are right. The statement "{ } doesn't define any object" is not clear enough. It should be improved.
thank you for your feedback!
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1207 :
Posted: Sun Jun 07, 2015 10:36 am
by zevsnoe
Hi Admin,
in your explanation
Object arr[][] = new Object[][] {new String[], {} };
will not compile since new String[] is not valid array initializer - it should contain size or elements, so sth like
new String[5] or new String[]{"string"} would work
Please fix, tnx
Re: About Question enthuware.ocajp.i.v7.2.1207 :
Posted: Sun Jun 07, 2015 7:47 pm
by admin
Fixed.
thank you for your feedback!