In this article we will learn about Array Declaration, Initialization and Iteration in Java. We will come to known ways of array declaration, assigning values to an array and iteration through an array to access values.

In Java arrays are objects that store multiple homogeneous variables of primitive or reference type. Being an object each array is stored on Heap as any other Java object, no matter if the array holds primitive variables or reference variables.

Declaring an Array in Java

In Java, Array declaration have a special syntax as in C/C++ like languages, to declare an array one need to start the statement with type of variable, the array is going to hold. It can be a primitive or a reference type. Than a pair of square brackets either to the left of identifier or right of identifier as shown in examples below:

Declaring single dimensional array


	// declaring array to hold integer type vriables
	int[] arr;
	int arr2[];

	// declaring array to hold reference variables of Impl class objects
	Impl[] implArr;
	Impl implArr2[];

Declaring multi dimensional array


	// declaring array to hold integer type vriables
	int[][] arr;
	int arr2[][];

	// declaring array to hold reference variables of Impl class objects
	Impl [][] implArr;
	Impl implArr2[][];


Above example is commonly used type of declaration; below are more valid but bit less readable array declaration types:


	// declaring three dimentional array
	Impl[][] implArr[];
	Impl[] implArr3[][];
	
	// declaring two dimentional array
	Impl []implArr2[];

Constructing an array in Java

Constructing an array means, creating an array object on heap with new keyword and assigning array object to created variable. One should assign a size to array, indicating how much number of variables the array can contain in it.

1) Using new keyword:

The most common way of constructing an array is using a new keyword and assigning a size to the pair of square brackets as shown in the example below:
		// declaring array to hold integer type vriables
		int[] arr;
		
		// constructing array object on heap with new keyword and assigning it
		// to variable
		arr = new int[4];
		// declaring and constructing array in single statement
		int[] arr = new int[4];

		// declaring array to hold Apple type variables
		Apple[] arr;

		// constructing array object on heap with new keyword and assigning it
		// to variable
		arr = new Apple[4];
		// declaring and constructing array in single statement
		Apple[] arr2 = new Apple[4];


Multi Dimensional arrays are nothing but simple arrays holding more arrays, below is the code for constructing multidimensional arrays:


		// multi dimentional arrays construction
		int[] arr[] = new int[4][];
We don?t need to give size to second dimension; JVM needs to know the size of first array only to allocate space on memory. This first array can hold as number of references of other arrays as the size at the time of construction.

In the above examples, if array is just constructed and no value is assigned, primitive type arrays hold their default values.

int arr[] = new int[4];
for (int i = 0; i < arr.length; i++) {
	System.out.println(arr[i]);
}
This will print 0, four times as default value for int.

And arrays of reference types points to null values, until any object is not assigned to array elements.

 Apple apple[] = new Apple[4];
 for (int i = 0; i < apple.length; i++) {
	System.out.println(apple[i]);
}
This will print null, four times as default value for any object in Java (this case Apple).

2) Assigning values to array at the time of Declaration

No matters if an array is of primitive type or of reference type, one can assign values directly at the time of declaration. Need to provide value in between curly braces separated with a comma (,) and the size of array will be the same as the number of elements as shown below:
		
		// assigning values at the time of declaration for premitive type
		int arr[] = { 1, 23, 4, 5 };

		// assigning values at the time of declaration for reference type
		Apple a = new Apple();
		Apple apple[] = { a, new Apple() };
We can also use direct assignment to multidimensional arrays, as shown in the example below:

int arr[][] = { { 1 }, { 23, 4 }, { 5 } };
Apple a = new Apple();
Apple [][] apple = { { a }, { new Apple(), a } };

3) Anonymous Array declaration in Java

Both primitive and referenced arrays can be created anonymously, the size should not be provided in this case, because size will be decided by the number of elements in between curly brackets, as shown below:

		int[] intArray = new int[] { 1, 2, 3 };
		int[][] intArrayTwoD = new int[][] { { 1 }, { 2, 3 } };

		Object[][] obj = new Object[][] { { new Object() },
				{ new Object(), new Object() } };
		Object[] objTwoD = new Object[] { new Object(), new Object(),
				new Object() };

In this article we came across, Arrays in Java, declaration, initialization and iterating through an array. In upcoming articles we will see more about Java and related technologies.
  • By Techburps.com
  • Mar 17, 2015
  • Core Java