The Java language has two data-type categories: primitive and reference. Array types are a special case of reference types. This means that when you create an array in Java it is an object just like a reference-type object. However, Java arrays possess special features over and above ordinary reference types. These special features make it easy to think of arrays as belonging to a distinct type category all by themselves. This section explains why Java arrays are so special.
When you declare an array in Java you specify an array type as shown in figure 8-2 above. Just like reference types, array types automatically inherit the functionality of the java.lang.Object class. Each array type also implements the Cloneable and Serializable interfaces. Figure 8-3 gives a UML diagram of an array-type inheritance hierarchy.
Figure 8-3: Array-Type Inheritance Hierarchy
When an array object is created in memory it will also have a corresponding java.lang.Class object. One Class object is shared by all array objects having the same array type. You will see how the Class and Object methods can be used in conjunction with array objects later in the chapter.
The java.lang.reflect.Array class has of a number of static methods that can be used to manipulate array element values. I will not cover the use of the Array class in this chapter although I encourage you to explore its functionality on your own by studying its methods and what they do.
The following table summarizes the special properties of Java arrays:
Property | Description |
---|---|
Their length cannot be changed once created. | Array objects have an associated length when they are created. The length of an array cannot be changed after the array is created. |
Their length can be determined via the length attribute | Array objects have a field named length that contains the value of the length of the array. To access the length attribute use the dot operator and the name of the array. For example:
int[] int_array = new int[5]; This code declares and initializes an array of integer elements with length 5. The next line of code prints the length of the int_array to the console:
System.out.println(int_array.length); |
Array bounds are checked by the Java virtual machine at runtime. | Any attempt to access elements of an array beyond its declared length will result in a runtime exception. This prevents mysterious data corruption bugs that can manifest themselves when misusing arrays in other languages like C or C++. |
Array types directly subclass the java.lang.Object class. | Because arrays subclass Object they have the functionality of an Object. |
Array types have a corresponding java.lang.Class object. | This means that you can call the java.lang.Class methods on an array object. |
Elements are initialized to default values. | Primitive type array elements are initialized to the default value of the particular primitive type each element is declared to contain. Each element of an array of references is initialized to null. |
Java array types have special functionality because of their special inheritance hierarchy. Java array types directly subclass the java.lang.Object class and implement the Cloneable and Serializable interfaces. There is also one corresponding java.lang.Class object created in memory for each array type in the program.