<< Chapter < Page Chapter >> Page >

One of the methods of an ArrayList object is the method named add . This method is used to add new elements to the end of the list.

Adding a Date object's reference to the list

Listing 5 calls the add method to add a new Date object's reference to the list.

(This is the method call that the compiler was complaining about in the warning in Figure 2 .)

When an object's reference is added to an ArrayList object (without the use of generic syntax), that reference is automatically converted to andstored as type Object .

(As you will see later, that is probably also true even with the use of generic syntax. I will have more to say about this later.)

What can you do with a reference of type Object?

I often tell my students that there are only twelve things that you can do with an object's reference that has been converted to type Object . The first eleven of those twelve things is to call any one of the elevenmethods that are defined in the Object class and inherited into all subclasses of Object . The twelfth thing is to attempt to cast the reference to some other type in an attempt to call some other method on thereference after casting.

Invoking getTime on the reference

The objective of the code in this program is to call the method named getTime on the Date object's reference. The getTime method is not one of the eleven methods that are defined in the Object class. Rather, the getTime method is defined in the Date class.

Change the type of the reference

Therefore, in order to call the getTime method on the reference after it is retrieved from the ArrayList object, the type of the reference must be converted from type Object to type Date . This is accomplished by the cast operation in the expression containedin the argument list for the println method in Listing 5 .

That expression calls the get method on the reference to the ArrayList object to retrieve the element stored at index 0 of the collection. The get method returns the reference as type Object (at least that was true prior to the introduction of generics). Then the expression casts the reference to type Date , converting its type back to type Date .

Invoking the getTime method

Finally, the expression successfully calls the getTime method on the reference of type Date .

The program produces an output similar to that shown below:

1377991758665

This is the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by the Date object.

Casting was a necessity prior to Java version 1.5

Prior to the release of Java version 1.5, it was always necessary to cast references retrieved from collection objects in order to call any methods onthem other than the eleven methods defined in the Object class.

Some authors refer to this casting requirement as "the drudgery of casting," and indicate that casting may be eliminated through generics.

(In my opinion, from this viewpoint alone, the cure is worse than the disease. Casting syntax is much simpler and more straightforward thangenerics syntax.)

May eliminate runtime errors and exceptions

However, it is possible for the programmer to perform an incorrect cast at this point in the program, which will usually result in a ClassCastException being thrown at runtime.

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