<< Chapter < Page Chapter >> Page >

Listing 5 creates a new ordinary object of class Array05 . The code assigns the object's reference to the first element in the array object of element type Object , referred to by the reference variable named v2 .

Listing 5 . A new ordinary object of class Array05.
v2[0] = new Array05();

This is allowable because the reference to an object of any class can be assigned to a reference variable of type Object .

(The array object referred to by v2 contains two elements, each of which is a reference variable of type Object .)

Populate the second element

The code in Listing 6 assigns the reference that points to the existing array object ofthe element type int to the second element in the array object of element type Object .

Listing 6 . Populate the second element.
v2[1] = v1;

This is allowable because a reference to any array object can be assignedto a reference variable of type Object .

Array contains two references

At this point, the array object of element type Object contains two references.

(Each of the elements in an array of the declared type Object[] is a reference of type Object .)

The first element refers to an ordinary object of the class Array05 .

The second element refers to an array object of type int , having five elements, populated with the integer values of 1 through 5 inclusive.

(Note that this is not a multi-dimensional array in the traditional sense. I will discuss the Java approach to such multi-dimensional arrays in the nextmodule. This is simply a generic array of element type Object , one element of which happens to contain a reference to an array object of type int .)

Print some data

The code in Listing 7 passes each of the references to the println method of the PrintStream class.

Listing 7 . Print some data.
System.out.println(v2[0]);System.out.println(v2[1]);

The println method causes the toString method to be called on each reference. The String returned by the toString method is displayed on the computer screen in each case.

This is allowable because any method defined in the Object class (including the toString method) can be called on any reference stored in a reference variable of type Object .

This is true regardless of whether that reference is a reference to an ordinary object or a reference to an array object.

The output

Listing 7 causes the following two lines of text to be displayed:

Array05@73d6a5 [I@111f71

Pretty ugly, huh?

In both cases, this is the value of the String returned by the default version of the toString method defined in the Object class. Here is what Sun has to say about that default behavior:

"Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for aperson to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, theat-sign character `@', and the unsigned hexadecimal representation of the hash code of the object."

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