posted 20 years ago
Array Loopholes
============================================================================
1 declaring int size in declaration statement
int a[23]; //invalid
int a[] = new int[25] // correct
============================================================================
2 calling length() method on arrays
Arrays dont have length method they have length variable
int a = new int[10];
System.out.println(a.length()); //invalid
System.out.println(a.length)); //valid
============================================================================
3 declaration and initialization with specying no of elements
int a []=new int[25]{1,2,3,4,5,6,7,8}; // error cant specify size as well as initialize it
============================================================================
4 arrays are always initialized to default values no matter where they are declarted even in local scope i.e. in methods too unlike other variables which have to be explicitly initialized.
============================================================================
5 calling size method or size field of array .
Arrays dont have size field or size() method. only length field.
============================================================================
6 trying to resize an array
Arrays cannot be resized
============================================================================
7 Foll statements are perfectly valid and give no compile time error
int a[] = new int[]{}; // no values in curly braces 0 length aray
int a[] = new int[]{,};// curly braces with comma in braces VALID
int a[] = {1,2,3,4,5,6,7,}; // perfectly valid
int[][] aMatrix = new int[4][];// other specified at runtime
============================================================================
8 The number of bracket pairs indicates the depth of array nesting
============================================================================
9 Arrays can have "abstract" classes as their component type.
The elements of such an array may have their value as null reference
or instances of any subclass of the abstract class that is not itself abstract
============================================================================
10 int[] a,b,c equivalent to
int a[],b[],c[]
============================================================================
11 If an array variable v has type A[] A is ref type
then v can hold a reference to an instance of any array B[] provided B can be assigned to A
============================================================================
12 Trying to acces array using long index value gives compile time error
int a[] = {1,2,3,4,5,6,7,8,9};
long l = 5;
System.out.println(a[l]); // compile time error
even if the long is final error occurs
int a[] = {1,2,3,4,5,6,7,8,9};
final long l = 5;
System.out.println(a[l]); // compile time error
============================================================================
13 These are valid
arr[bytevalue],arr[charvalue],arr[intvalue],arr[shortvalue]
============================================================================
14 Array accesses are checked at runtime an not compiletime
So at runtime arrayindexoutofbounds is thrown
int a[] ={1,2,3,4,5,6,7}
System.out.println(a[45]);//compiles fine but gives runtime error
============================================================================
15 Arrays implement cloneable and serializable interfaces
============================================================================
16 Primitive array types can never be assigned to each other, eventhough the primitives themselves can be assigned. ie., ArrayofLongPrimitives = ArrayofIntegerPrimitives gives compiler error eventhough longvar = intvar is perfectly valid
============================================================================
17 arrays 'length' field is final so cannot assign new value to it
arr.length = 20; //invalid
When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.