<< Chapter < Page | Chapter >> Page > |
Array object is not a subclass of class Object
An array object can be treated as type Object for purposes of calling the methods of the Object class on the reference to the array object. However, it would probably be misleading to say that an array object isinstantiated from a subclass of the Object class.
The new operator and the constructor name
Ordinary objects are created by applying the new operator to the constructor for a class, where the name of the constructor is always the same asthe name of the class. That is not the case with array objects. Array objects are created by applying the new operator to the name of the type of data to be encapsulated in the array object.
Passing parameters versus square-bracket notation
In addition, whereas the instantiation of ordinary objects involves parameters passed in parentheses, a square-bracket notation is used instead ofparentheses to create an array object. The value in the square brackets specifies the length of the array.
Creating an array object
Array objects (with default initialization values) are created by applying the new operator to the name of the data type to be stored in the array, using a square-bracket notation. An example is shown by theright-hand portion of the first statement in Listing 3 .
Listing 3 . Creating array objects. |
---|
int[] x1 = new int[5];
int[]x2 = {1,2,3,4,5}; |
A five-element array object
The first statement in Listing 3 creates an array object capable of storing five values of type int . The statement also assigns the array object's reference to the newly-declared reference variable named x1 .
Default initial values
Each element in the array is initialized to the default value zero.
(All array elements created in this manner receive a default initial value. Numeric primitive types receive an initial value of zero. Elements oftype boolean receive an initial value of false . Elements whose type is the name of a class or the name of an interface receive an initial valueof null .)
Explicit initialization
The second statement in Listing 3 also creates an array object capable of storing five values of type int , but in this case, the values in the elements are explicitly initialized to the values shown.
(Note that the new operator is not used in the second statement in Listing 3 . This is also a significant departure from the syntax used to instantiate ordinary objects.)
This array object's reference is assigned to the reference variable named x2 .
Note the empty square brackets in the variable declarations
The syntax of the type specification for the reference variable in each statement in Listing 3 is different from the syntax used in the type specification for either aprimitive variable or an ordinary class type reference variable (note the square brackets on the left in Listing 3 ) . In Listing 3 , the type specifications indicate that each variable is capable of holding areference to an array object.
The size of the array
Furthermore, the empty square brackets (in the declaration of the reference variable) indicate that the reference variable doesn't know (and doesn't care) about the size of the array to which it may refer. Each of the reference variables declared in Listing 3 can refer to a one-dimensional array object of any size. Also, each of the referencevariables can refer to different array objects at different points in time during the execution of the program.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?