wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how is this correct Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how is this correct" Watch "how is this correct" New topic
Author

how is this correct

sree javab
Greenhorn

Joined: May 05, 2008
Posts: 9
Object [] object = new String[5][5];

String array is 2 D, whereas object array is 1D...it is compiling??
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Look at a 2D array as an array of arrays. An array is considered to extend Object (take a look at the methods available to an array to see this in action). Therefore, an array of arrays is an array of Objects. It's like doing this:
Komal Arora
Ranch Hand

Joined: Sep 30, 2010
Posts: 91

Arrays are objects. Hence the String array get's assigned to the object. the code will compile for object being 1D and 2D, but will produce an error when you declare the object array as 3D.


OCPJP
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Komal Arora wrote:Arrays are objects. Hence the String array get's assigned to the object. the code will compile for object being 1D and 2D, but will produce an error when you declare the object array as 3D.


Are you sure? This works for me:

As does this:

And this:
Komal Arora
Ranch Hand

Joined: Sep 30, 2010
Posts: 91

i meant that

Object o[][][] = new String[5][5]; would give the compilation error.
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
Object [] object = new String[5][5];

String array is 2 D, whereas object array is 1D...it is compiling??


Interesting. Well, here goes. An array is an Object and A String is an Object. An array of Strings is an object. An Object array is again an object. So as long as you keep the Object array size less than the String array size I think it'll work.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how is this correct
 
Similar Threads
one more certpal question
exam lab2(question 34)
java
class cast exception
Object and array ref