<< Chapter < Page Chapter >> Page >

Doesn't address array objects

Obviously, this description of behavior doesn't address the case where the object is an array object, unless the characters [I are considered to be the name of a class. (I will have a little more to say about this later.)

Produce some more output

Finally, Listing 8 shows the last statement in this simple program.

Listing 8 . Produce some more output.
System.out.println( ( (int[])v2[1])[4] );}//end main }//end class Array05

What does this mean?

As you can see, the syntax of this statement is pretty ugly.

Values are accessed from an array object by following the array's reference with a pair of square brackets containing an integer index value as follows:

v2[1]

Get the value at index 1 as type Object

This code begins by accessing the component at index value 1 of the array object referred to by the reference variable named v2 .

The value retrieved is a reference, and is retrieved as type Object , (because the variable named v2 was declared to be of type Object[] ).

A cast is required

A cast is used to convert from type Object[] to type int[] using the following code:

(int[])

This produces a reference to an array object capable of containing values of type int .

Apply index to the int array

After the type of the reference has been converted, the accessor [4] is applied to the reference. This causes the int value stored in the array object of type int (at index value 4) to be returned.

(If you refer back to Listing 4 , you will see that the integer value 5 was stored in the element at index value 4 of this array object.)

You should try to remember this syntax and compare it with the syntax used in the Java approach to traditional multi-dimensional arrays, which I will discussin the next module.

The output

Thus, the code in Listing 8 causes the number 5 to be displayed on the computer screen.

Let's recap

To recap, the program named Array05 creates a two-element array object capable of storing references of type Object .

Object is generic

Because Object is a completely generic type, each of the elements in the array is capable of storing a reference to any ordinaryobject, or storing a reference to any array object.

Store reference to ordinary object in generic array

The first element in the array is populated with a reference to an ordinary object instantiated from the class named Array05 .

(Important: The actual object does not occupy the array element. Rather, the actual object exists someplace else in memory, and a reference to the objectoccupies the array element.)

Store a reference to an array object in the generic array

The second element in the array of element type Object is populated with a reference to another array object capable of containing elements of type int .

As above, the actual array object of type int does not occupy the second element. Rather, that array object exists someplace else in memory, and areference to the array object occupies the second element in the array of element type Object .

Display some data

After the array object of element type Object is created and populated, three print statements are executed to display information about thearray object and its contents (those print statements are shown in Listing 7 and Listing 8 ).

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask