This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array Issue 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 "Array Issue" Watch "Array Issue" New topic

Array Issue

Henry Zhi Lin
Ranch Hand

Joined: Nov 04, 2008
Posts: 69
Hi guys,

I need a favour from you.

I saw the following code in Inquisition John Meyers' exam

My answer is this print null, but the answer in the system is compile error, b is a two dimension array a has one dimension? I need your explaination.

I actually test this in eclipse, the result just like I said print null.

SCJP 5.0
Prabhat Gupta
Ranch Hand

Joined: Jan 22, 2008
Posts: 135
int a [] = null, b [] = null;

we can understand this as
int a[]=null;
int[]b[]=null ;

since a two dimensional array can be written as
int[][]b or int[]b[] or int b[][] all these declarations are valid .Also,

we can't make reference of two dimensional array to point one dimensional array,as both are totally diffent object. hence we are getting compilation error at line

[ November 07, 2008: Message edited by: Prabhat Gupta ]
Henry Zhi Lin
Ranch Hand

Joined: Nov 04, 2008
Posts: 69

Thanks for your answer, I understand your point, but this puzzled me because when I tested in Eclipse, it does not return any errors and prints null. I guess the compiler of my Eclipse indeed treats this code separately as two one dimension array.
[ November 07, 2008: Message edited by: Henry Zhi Lin ]
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137

The following code willnot compile because we are trying to assign one dimension array to 2 dimension array! (here [] is placed next to "int")

But in the code which is posted above: (here [] is placed next to variable name)

There is nothing wrong. It will compile and give null as output.

@Prabhat: Could you please explain why you have considered the array b as 2 -dimension array here?

Thanks,<br />Srilatha M
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38075
There is no such thing in Java as a 2-dimensional array, but there is such a thing as an array of arrays, eg int[][]. That is an array of arrays.
There is a way of creating an array and an array of arrays in the same statement

int[] a = null, b[] = null;

That is probably what you thought you wrote, and in combination with the rest of your code will produce an error.

By the way: I would regard that bit of code as obscure and hard to read and bad style.
Amit Ghorpade

Joined: Jun 06, 2007
Posts: 2716

M SRILATHA could you please adjust your display name so that it does not contain all upper case letters, like M Srilatha.


|Asking Good Questions|
Henry Zhi Lin
Ranch Hand

Joined: Nov 04, 2008
Posts: 69

You are right, I checked again, just as you said, I put the [] in wrong place. Thanks for pointing out for me.
Amit Ghorpade

Joined: Jun 06, 2007
Posts: 2716

"Srilatha M" please check your private messages for an important administrative matter.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Array Issue
Similar Threads
Values of Hash Map to be 2-dimensional array
Your inputs
Array Question
multi dimension array
2D array