• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Your inputs

 
sathish kumar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • 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!
 
Sagar Rohankar
Ranch Hand
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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 ]
 
Sumit Gaikaiwari
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Awesome, I can't believe I did not read through that question.

Thanks for the explanation folks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic