<< Chapter < Page Chapter >> Page >

Array objects

I haven't discussed array objects up to this point in this collection. That is the purpose of this module.

Also tends to complicate

The existence of array objects also tends to complicate the OOP structure of a Java program consisting only of ordinary objects. Even if you don't considerarray objects to be a different kind of object, you must at least consider them to be a special kind of object. A completely different syntax is required to create array objects than the syntax normally used to instantiate ordinaryobjects.

References to array objects

Arrays are objects in Java (at least, arrays are always encapsulated in objects). Array objects are dynamically created. Like ordinary objects, array objects are accessed via references. The reference to an array object maybe assigned to a reference variable whose type is specified as:

TypeName[]

For example, Listing 1 shows some unrelated declarations for variables that are capable of storing references to arrayobjects.

Listing 1 . Sample variable declarations for array objects.
int[] x1;Button[] x2;Object[] x3;

Note the empty square brackets that are required in the variable declarations in Listing 1 .

The special case of type Object

In addition, a reference to an array object may be assigned to a reference variable of type Object as shown in Listing 2 , where x4 is a reference to an array object.

Listing 2 . The special case of type Object.
Object x4;

Note that there are no square brackets in the statement in Listing 2 .

What does this mean?

This means that like ordinary objects, a reference to an array object can be treated as type Object (with no square brackets).

This further means that any of the methods defined in the Object class (such as the toString and getClass methods) can be called on a reference to an array object.

The String representation of an array object's reference

For example, when the toString method is called on a reference to an array object containing data of type int , the resulting string will be similar to the following:

[I@73d6a5

Pretty ugly, huh?

You may recognize this as being similar to the default String returned by calling the toString method on an ordinary object with the name of the class for the ordinary object being replaced by [I .

For example, the String returned by calling the toString method on an object of the class named Array04 , (with no overridden toString method), looks something like the following.

Array04@73d6a5

(Note that the hexadecimal numeric values following the @ in both of the above examples will change from one case to the next.)

Calling the getClass method

Similarly, calling the getClass method on references to array objects containing data of the types Array04 , Button , and int , respectively, and then calling the toString method on the Class objects returned by the getClass method, produces the following:

class [LArray04; class [Ljava.awt.Button;class [I

Complicating the OOP structure

I made the following statement in an earlier paragraph:

"The existence of array objects also tends to complicate the OOP structure of a Java program consisting only of ordinary objects."

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