Arrays in Java II: Declaring, Initializing, Instantiating Arrays

There are multiple ways of declaring and initializing arrays. This abundance is both a boon and bane. It allows you to write your array related code in multiple ways. But this richness is also a tipping point for beginners. Having already been introduced to Java arrays, we now see how to declare, initialize and instantiate an array.

Declaring an Array

Declaring a variable is the act of telling a compiler what type of data is going to be stored at a particular memory location.

Java allows you to declare arrays two ways. The [] is the essential feature of an array declaration. It tells the compiler that the data type is an array. The [] can either be place after the name of the variable or after the data type. For example, if you want to declare an array of ints, you can do it in the following couple of ways  –

1 2int[] declaredArray1; int declaredArray2[];

Both forms are equivalent and valid and are used in production code. (To the extent that arrays are used in production code).

If you want to pick one over theother stick with int[] format because it is clearer in the sense that it says that the type is an array of int.

Initializing an Array

In Java, you can populate an array at the time of or right after declaration. This act of providing initial values to be put inside an array is called initializing an array.

You might be tempted to write the following (actually, quite intuitive) code to initialize a Java array –int[] fibonacci = [1, 1, 2, 3, 5, 8];

But unfortunately, it does not work. And the reason it does not work is that [] in Java are a declaration rather than initialization construct. The intial values cannot be contained inside [ ].

You might also be tempted to write the following code –int[6] fibonacci = { 1, 1, 2, 3, 5, 8 };

// or

int fibonacci[6] = { 1, 1, 2, 3, 5, 8 };

This also does not work because the compiler does not like our specifying the size of the array and the values in one statement. If you are giving a set of values for an array, you should let the compiler decide the size of array. Allowing statements such as the one shown above would have left loopholes in Java design. For example, you would have to deal with mismatches between declared sizes and the number of values specified. What if someone declares array size as 6 but gives 7 or more values? What if they give only 5?  What should be stored at the extra slot? Java avoids all these issues by simply disallowing the above syntax.

Java arrays are initialized using array initializer block. An initializer block is a code block delimited by {  and } and contains comma separated values. The initializer block itself does not contain a semi-colon. The initialization statement, like any other statement must terminate with a semi colon. Here is the general structure –Type[] variableName =  Array Initializer Block ;

// OR

Type variableName[] =  Array Initializer Block ;

The array initializer block has the following general structure –{  // beginning of code initializer block

value1,   // A ‘ , ‘ separates the values to be filled in array

value2,

// and so on

lastValue

// note there is no comma after last element

}

It is also possible to have separate statements for array and declaration. The general strucuture in such a usage is –Type[] arr;

// or

Type arr[];

// and somewhere down the line

arr = new Type[] { };

// the above statement shows instantiation of array

// (discussed in next section)

// followed by array initializer block

Examples of Array Initialization

The following code snippets exemplify how Java arrays are initialized –

Note that assignment such as the one shown in next example is valid. But it is not initialization. It is just an assignment.

1 2 3 4 5 6 7String[] bestWebsite = new String[3]; bestWebsite[0] = “Coding” ; bestWebsite[1] = “Raptor”; bestWebsite[2] = “.com”;

Instantiating an Array

An array, like all other objects in Java is instantiated using the new keyword.

The new keyword not only allocates the memory but also initializes the object.

1int[] arr = new int[8];

This statement not only declares an array by the name arr but also reserves the memory for 8 integers. arr has the reference to the memory location reserved by new. The new mechanism is the only way in Java for reserving memory for objects.

Till now we have seen only one dimensionaly arrays. In the next article we will examine multidimensional arrays.

Leave a comment

Your email address will not be published.