aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Array declaration" Watch "Array declaration" New topic
Author

Array declaration

Sakthi Kani
Ranch Hand

Joined: Mar 29, 2005
Posts: 98
hi

Please any one can explain me in the following code,
why the line 2 give compliler error.....
----------------------------------
int[] a1[],a2[]; // 1
int []a3,[]a4; // 2
int []a5,a6[]; // 3
int[] a7,a8[]; // 4
-----------------------------------
The line2 similar to

int []a3;
int []a4;

Then why it leads to compiler error.


" Don't be afraid of pressure. Remember that pressure is what turns a lump of coal into a diamond... " <br /> <br />Thanks & Regards...<br />Sakthi<br />SCJP1.4, OCA
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi,
In line 2 any valid identifier is expected here. So compiler throws an error message.

int []a3, []a4; // 2
change this line to
int [a3],a4[]; // It compile without any error message


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
J Abraham
Ranch Hand

Joined: Jun 25, 2004
Posts: 101
Hai shakti,

The declaration of the array

int []a3,[]a4; // 2

shows compile time error as the array brace has to be put after the variable. the array a3 declaration is correct since it immedietly follows the datatype, whereas a4 comes after coma. the correct declaration would be

int []a3,a4[]; // 2

Hope this solves ur doubt.

Cheers,

-jibiN


have will, have everything...


<b>J Abraham</b>
SCJP5,SCWCD1.4
Sakthi Kani
Ranch Hand

Joined: Mar 29, 2005
Posts: 98
Hi

I am still not clear..

int a1,a2;

Both a1 and a2 are type int.

Then why not in arrays as,

int []a1,[]a2;
plz help me...
Manish Nijhawan
Greenhorn

Joined: Apr 04, 2005
Posts: 10
Hi,

int [] a1;
the above statement means that a1 is of type int[].

int[] a1 , a2;
The above statement means that a1 and a2 both are of type int[].

so int[] becomes the type and a1 and a2 becomes variables.

if we write int[] a1[];
this means a1 is an array of type int[](which itself is an array) therefore a1 is of type int[][] i.e. a 2-d array.

but if we write int[] a1 , []a2;
here a1 is the name of the variable but []a2 cannot be resolved as the compiler expects a variable name and [] are not allowed in the name of the variable.


Manish Nijhawan<br />B-Tech
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi
is a2 is two dimensional while a1 is single dimnesional one.
Pally Gharmount
Ranch Hand

Joined: Aug 25, 2004
Posts: 34
simple

int []a3,[]a4; // is wrong, need a primitive/object type on left of []a4.

can be fixed by...

int []a3,a4[];

or

int []a3;
int []a4;

Pally
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array declaration
 
Similar Threads
arrays
exception doubt
Dan's Mock test on Mutil-dimensional arrays
Arrays
multidimensional array