programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

Ranch Hand
Posts: 47
• Number of slices to send:
Optional 'thank-you' note:
Dear Ranchers- This is a question that I pulled from John Meyer's SJCP question bank

int[] a = null , b [] = null;
b = a;
System.out.println( b );

Answer explanation shows "b is a two dimension array. a has one dimension", I did not understand how b[] is two dimensional array.

-Thanks!

Ranch Hand
Posts: 2908
1
• Number of slices to send:
Optional 'thank-you' note:

In Java , array can be defined as
DataType[] vaiable = new DataType[SIZE];
OR
DataType vaiable[] = new DataType[SIZE];

Which can be read as Array of 'DataType' of size 'SIZE'

So in your case ,when you split that compoun statment into one
You ll see , b is actually defined as :

int[] b[] = null; Which is two dimensional array OR array of array !

Hope this clarifies a little bit !
[ July 31, 2008: Message edited by: Sagar Rohankar ]

Greenhorn
Posts: 19
• Number of slices to send:
Optional 'thank-you' note:
If you see start of line, its "int[] ....", so every variable declared afterwards will be one dimensional array of type int, so the declaration
int[] a => a is an one dimensional array that can take int members

but, int[] b[] => makes b two dimensional array, as this declaration is equivalent to "int b[][]", it's an example of how weired Java syntaxes can be...

As an advice, pay very close attention to such pitfalls (though this is not a compiler error), scan your question like a compiler first before figuring out what logic is doing, many times, you can rush directly to option "won't compile" without looking at logic...

sathish kumar
Ranch Hand
Posts: 47
• Number of slices to send:
Optional 'thank-you' note:
Awesome, I can't believe I did not read through that question.

Thanks for the explanation folks.