<< 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 phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
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