<< Chapter < Page Chapter >> Page >

Casting errors are likely

This is a scenario where the programmer is likely to make a casting error when casting elements retrieved from the collection. Unless the programmeruses the instanceof operator to determine the type of a retrieved reference prior to performing the cast, the programmer is depending on hismemory to know the type of each reference in the population on the basis of the index of the element. If the programmer loses track of the types of thedifferent references with respect to the element's indices, a casting error is a strong possibility.

The error scenario

This is the error scenario depicted by the first call to the println method in Listing 6 . In this case, the element at index 0 is a reference to a Date object, and the element at index 1 is a reference to a String object. However, the programmer mistakenly retrieves the element at index 1 and attempts to castit to type Date , which is the type of the reference at index 0. This results in the runtime exception shown in Figure 3 .

Of course, it has always been possible to use the instanceof operator to confirm the type of a reference before performing a cast as a way to avoidthis type of programming error. Good programming practice would dictate the use of that construct when working with references to objects of mixed typesin a single collection.

Dealing with angle brackets in cnxml body text

You are reading this module at cnx.org. Documents published at cnx.org are maintained in a special flavor of xml know as cnxml. Without getting into thedetails, I will tell you that creating cnxml is not an easy task. In order to avoid having to write these modules in raw cnxml code, I originally write themusing a WYSIWYG xhtml editor and then run the xhtml files through a program of my own design that transforms the xhtml document into cnxml.

Unfortunately, my transform program is incapable of handling angle brackets in the body text of the document. Therefore, whenever thebody text in this document needs a left angle bracket, I will indicate the existence of a left or right angle bracket using a text description of the anglebracket. The code in the listings,however, will show the left and right angle brackets intact as required.

Avoiding the requirement to cast through the use of generics

The program named Generics03 shown in Listing 7 illustrates how generics can be used to avoid the requirement to cast references when they are retrieved from acollection.

(While casting is not difficult, avoiding the requirement to cast can also avoid the possibility of casting incorrectly.)

As in the previous programs, the main method in this program instantiates an object of the class named Generics03 and calls the method named runIt on the object.

Instantiate an ArrayList object

Also, as in the previous programs, this program declares an instance variable named var1 and initializes that variable with a reference to a new objectof type ArrayList . However, the syntax that is used for this purpose in this program is significantly different from the syntax used for thesame purpose in the previous two programs.

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