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

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
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