Skip to main content

Arrays Summary

  1. Array is an object in Java.
  2. You can’t specify the size of an array at the time of declaration.
int a[];// VALID
int a[1];// INVALID
  1. Array size is mandatory while constructing an array object.
int a[]=new int[3];// VALID
int a[]=new int[];// INVALID
  1. Size of an array must be int compatible type (byte, char, short, int).
  2. Size of an array must be in the range of 0 to 2147483647.
  3. Size of an array can be provided using a variable.
  4. If you specify a negative value as array size β†’ ➑ java.lang.NegativeArraySizeException (runtime)
  5. If sufficient memory is not available β†’ ➑ java.lang.OutOfMemoryError (runtime)
  6. Elements of an array can be accessed using index representation.
  7. Index range is from 0 to size-1.
  8. If array size is 0 β†’
  • No elements allocated
  • You cannot access any index
  1. Accessing an invalid index β†’ ➑ java.lang.ArrayIndexOutOfBoundsException
  2. Accessing array using a null reference β†’ ➑ java.lang.NullPointerException
  3. Arrays are static in nature β†’ once created, size cannot be changed.
  4. Array reference variable can be modified.
  5. If array reference is declared as final β†’
  • Reference cannot be changed
  • But elements can be modified
  1. Since array is an object, reference can be stored in Object type
int arr[]=new int[5];
Object obj=arr;// VALID
  1. With Object reference, index representation can’t be used
obj[0];// INVALID
  1. You can assign one array into another if both are of same type
int arr1[]=new int[2];
int arr2[]=arr1;// VALID

πŸ’‘ Quick Understanding​

  • Array = object + fixed size
  • Index always starts from 0
  • Most common errors:
    • NullPointerException
    • ArrayIndexOutOfBoundsException
    • NegativeArraySizeException

Arrays – Additional Points

20)​

You can’t assign one array into another array when both arrays are of different types

byte arr1[]=new byte[2];
int arr2[]=arr1;// INVALID

21)​

The class for the array will be generated at runtime based on the dimension and data type.

22)​

When you assign one array to another, the array address is copied, so both references point to the same array object.

23)​

If you modify elements using one reference, the changes will be reflected in the other reference as well.

24)​

You can’t use the + operator with an array reference variable

int arr[]=new int[3];
(arr+1);// INVALID

25)​

When constructing a multi-dimensional array, the first dimension is mandatory, and other dimensions can be ignored.

Example (a)​

int arr[][]=new int[3][2];

βœ” Creates 3 arrays, each of size 2

Example (b)​

int arr[][]=new int[3][];

arr[0]=new int[2];
arr[1]=new int[3];
arr[2]=new int[4];

βœ” Creates 3 arrays with sizes:

  • arr[0] β†’ 2 elements
  • arr[1] β†’ 3 elements
  • arr[2] β†’ 4 elements

πŸ’‘ Quick Understanding​

  • Arrays of different types cannot be assigned
  • Array assignment = reference copy (not data copy)
  • Changes reflect across references βœ”
  • Multi-dimensional arrays β†’ jagged arrays supported