<< 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.

Questions & Answers

if three forces F1.f2 .f3 act at a point on a Cartesian plane in the daigram .....so if the question says write down the x and y components ..... I really don't understand
Syamthanda Reply
hey , can you please explain oxidation reaction & redox ?
Boitumelo Reply
hey , can you please explain oxidation reaction and redox ?
Boitumelo
for grade 12 or grade 11?
Sibulele
the value of V1 and V2
Tumelo Reply
advantages of electrons in a circuit
Rethabile Reply
we're do you find electromagnetism past papers
Ntombifuthi
what a normal force
Tholulwazi Reply
it is the force or component of the force that the surface exert on an object incontact with it and which acts perpendicular to the surface
Sihle
what is physics?
Petrus Reply
what is the half reaction of Potassium and chlorine
Anna Reply
how to calculate coefficient of static friction
Lisa Reply
how to calculate static friction
Lisa
How to calculate a current
Tumelo
how to calculate the magnitude of horizontal component of the applied force
Mogano
How to calculate force
Monambi
a structure of a thermocouple used to measure inner temperature
Anna Reply
a fixed gas of a mass is held at standard pressure temperature of 15 degrees Celsius .Calculate the temperature of the gas in Celsius if the pressure is changed to 2×10 to the power 4
Amahle Reply
How is energy being used in bonding?
Raymond Reply
what is acceleration
Syamthanda Reply
a rate of change in velocity of an object whith respect to time
Khuthadzo
how can we find the moment of torque of a circular object
Kidist
Acceleration is a rate of change in velocity.
Justice
t =r×f
Khuthadzo
how to calculate tension by substitution
Precious Reply
hi
Shongi
hi
Leago
use fnet method. how many obects are being calculated ?
Khuthadzo
khuthadzo hii
Hulisani
how to calculate acceleration and tension force
Lungile Reply
you use Fnet equals ma , newtoms second law formula
Masego
please help me with vectors in two dimensions
Mulaudzi Reply
how to calculate normal force
Mulaudzi
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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