This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Your inputs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Your inputs" Watch "Your inputs" New topic
Author

Your inputs

sathish kumar
Ranch Hand

Joined: Feb 14, 2007
Posts: 47
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

Joined: Feb 19, 2008
Posts: 2902
    
    1



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 ]

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Sumit Gaikaiwari
Greenhorn

Joined: Sep 24, 2007
Posts: 19
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...


~Sumit<br /><a href="http://faq.javaranch.com/view?ScjpWallOfFame" target="_blank" rel="nofollow">SCJP Wall of Fame</a>
sathish kumar
Ranch Hand

Joined: Feb 14, 2007
Posts: 47
Awesome, I can't believe I did not read through that question.

Thanks for the explanation folks.
 
Consider Paul's rocket mass heater.
 
subject: Your inputs