byte [] arry1, array2[]; byte array3 [][]; byte[][] array4; If each array has been initialized, which statement will cause a compiler error? A. array2 = array1; B. array2 = array3; C. array2 = array4; D. both A and B E. both A and C F. both B and C
The answer is A. Compile it and see. The variable arry1 is a one-dimensional array. All of the others are two-dimensional arrays. [ July 02, 2002: Message edited by: Corey McGlone ]
arry1 is a single array, all other arrays are array of arrays, or two dimensional in this case. the assignment at A produces a compiler error because they are not compatible. incompatible types!
The answer should be F <BR> because both array1 and array2 <BR> are single dimentional arrays, so cast is ok<BR> when we try to cast two dimentional arrays <BR>to single dimentional arrays, like with <BR> the choice B, and C, the compilation <BR> error occured.
to verify, check javadoc utility<BR> A Java Student Saif Ullah SCJP2
Originally posted by Saif Ullah: The answer should be F because both array1 and array2 are single dimentional arrays, so cast is ok
No, they aren't. array1 is a one dimensional array and array2 is a two dimensinal array. From the JLS, §10.2 Array Variables:
The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both, as in this example:
The assignment has nothing to do with the size of the array. you only copying the reference. after the assignment, the two references point to the same array object.