Array Initialization An array in Java is a type of object that can contain a number of variables. These variables can be referenced only by the array indexa nonnegativeinteger. The first element in an array has an index of 0. All of these contained variables, or elements, must be the same type, whichis the type of the array. Every array has an associated lengthvariable, established when the array is created, which you can access directly. If you try to initializing array in java an element with an index that is outside the range of thearray, an exception is generated. Java arrays are one dimensional, but an arraycan contain other arrays, which gives the effect of multiple dimensions. You can have arrays of any of the Java primitives or reference variables. Theimportant point to remember is that when created, primitive arrays will havedefault values assigned, but object references will all be null. Declaration Like other variables, arrays must be declared before you use them. Declaration can be separate from the actual creation of the array. Here are someexamples of declaring variables that are arrays of primitives lines 1 through3 and objects lines 4 and 5 : 1. The statement that constructs anarray must give the size, as shown in the following code fragment, assumed tofollow lines 1 through 6 in the previous code the code in line 9 assumes thatan integer primitive nStrings has been initialized : 7. The array reference variables will have references to array objectsof known types. In other words, the type of an array object controls what can bestored in the indexed locations. You can test the type of an array variable withthe instanceof operator, using a initializing array in java for the reference type that lookslike an array declaration. For example, using the flags variableinitialized in line 8, the following test would result in true: 10. Integer and floating-point primitive arrays have elements initialized tozero values. Arrays of boolean types have elements of false values. Arrays of object types have null references. You can combine declaration of an array variable with construction, as shownin the following code examples: 1. Arrays of primitives have elements that are initialized to default values. Arrays of objects have the value null in each element. You are practicallyguaranteed to have a related question on the exam. Combined Declaration, Construction, and Initialization Java allows a statement format for combined declaration, construction, andinitialization of arrays with literal values, as shown in the following codeexamples note that the String array defined in lines 2, 3, and 4 istwo dimensional : 1. In the following code,we declare and create an array of Rectangle objects, and then createthe Rectangle objects for each element: 1. } The Java compiler checks the assignment of values to array positions justlike it checks assignment to single variables. For example, the following codewould not compile because the compiler knows that 1024 is outside therange of byte variables. Anattempt to use a bad index in an array operation results in an ArrayIndexOutOfBoundsException being thrown. This example caused many errors in mock exam tests. Most people forget that Boolean is a wrapper class for boolean values and thus the array creation statement in line 2 merely created the array. All of the references in that array are initialized to null.