• Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Your inputs

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome, I can't believe I did not read through that question.

Thanks for the explanation folks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic