Array Declaration

The following are the ways to declare an array.

 int [] a;
 int a[];
 int [] a;

The first one is recommended to use because the type is clearly separated from the name.At the first time of declarations we are not allowed to specify the size. Violation leads to compile time error.

int[] a;//valid
int[6] a;//invalid

Declaring Multidimensional Arrays
The following are the valid declarations for multidimensional arrays.

	int[][] a;
	int a[][];
        int [][]a;
	int[] a[];
	int[] []a;

we can specify the dimension before name of variable also, but this facility is available only for the first variable.

int[] a[],b[];//valid
int[] []a,[]b;//invalid

Construction of Arrays

  1. Single Dimension : Arrays are internally implemented as object hence by using new operator we can construct an array.Compulsory at the time of construction we should specify the size otherwise compile time error.
  2. int[] a = new int[10];//valid
    int[] a = new int[];//invalid
  3. It is legal to have an array with size 0 there is no compile time error or run time error.
  4. int[] a = new int[0];
  5. If we are specifying array size with some –ve integer we will get run time exception saying NegativeArraySizeException.
  6. int[] a = new int[-10];
  7. The only allowed Data type to allow the size are byte, short,char,int. if we are using any other datatype we will get a compile time error.
  8. int[] a = new int[10];
    int[] a1 = new int[100]; 
    int[] a = new int[10L]; // compile time error: possible loss of precision found: long
    required: int
    int[] a = new int[10.5];// compile time error
    int[] a = new int[true]; // compile time error: Incompatible types found : boolean required:int.
  9. The maximum allowed Array Size in java is 2147483648.

Multi Dimension: In java multidimensional arrays are implemented as single dimension arrays. This approach improves performance with respect to memory.

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



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


declaring an array for the following diagram.

a[][][] = new int[2][][];
a[0] = new int[3];
a[0][1] = new int[1];
a[0][2] = new int[2];
a[0][3] = new int[3];
a[1] = new int[2][2];

Initialization of arrays

Once we created an array all it’s elements initialized with default values.


int[] a = new int[3];
System.out.println(a[0]);//Output: 0
System.out.println(a);//O/P: [I@12dacd1


int[][] a = new int[3][2];
System.out.println(a); // [I@10b62c9
System.out.println(a[0]); // [[I@82ba41
System.out.println(a[0][0]);// 0


int[][] a = new int[3][];
System.out.println(a[0]); //null
System.out.println(a[0][0]); //NullPointerException

Once we created an array all it’s elements are initialized with default values.If we are providing any explicit initialization then default values will be overridden with our provided values.


int[] a = new int[3];
a[0] = 10;
a[1] = 20;
System.out.println(a[0] + "---" + a[1] + "---" + a[2]);
a[10] = 100; //RuntomeException: ArrayIndexOutOfBoundsException.
a[-10] = 100; //RuntomeException: ArrayIndexOutOfBoundsException.
a[10.5] = 100; //compile time error: PossibleLossOfPrecision found : double
                                                             required : int

when ever we are trying to access an array with int index which is not in valid range then we will get runtime exception saying “ArrayIndexOutOfBoundsException” but there is no compile time error.
If we are trying to access an array index with the following datatype we will get compile time error.
float, double, long, boolean.