<< Chapter < Page Chapter >> Page >

Back to Question 6

Answer 5

B. Runtime Error

Explanation 5

ClassCastException

There are some situations involving casting where the compiler cannot identify an erroneous condition that is later identified by the runtime system.This is one of those cases.

This program begins with an array of type double []. Thereference to that array is converted to type Object . Then it is cast to type String []. All of these operations are allowed bythe compiler.

However, at runtime, the runtime system expects to find references to objects of type String in the elements of the array. What it finds instead is values of type double stored in the elements of the array.

As a result, a ClassCastException is thrown. Since it isn't caught and handled by the program, the program terminateswith the following error message showing on the screen.

java.lang.ClassCastException: [D at Worker.doArrays(Ap068.java:17)at Ap068.main(Ap068.java:6)

Back to Question 5

Answer 4

C. 1.0 2.0

Explanation 4

Finally, we got it right

Finally, we managed to get it all together. The program compiles and executes correctly. This program illustrates the assignment of an array object'sreference to a reference variable of type Object , and the casting of that reference of type Object back to the correct array type in order to gain access to the elements in the array.

But don't go away, there is a lot more that you need to know about arrays in Java. We will look at some of those things in the questions that follow.

Back to Question 4

Answer 3

A. Compiler Error

Explanation 3

Must use the correct cast syntax

While it is possible to store an array object's reference in a reference variable of type Object , and later cast it back to an array type to gain access to the elements in the array, you must use the correctsyntax in performing the cast. This is not the correct syntax for performing that cast. It is missing the empty square brackets required to indicate a reference to an array object.

A portion of the compiler error produced by JDK 1.3 is shown below:

Ap066.java:17: inconvertible types found : java.lang.Objectrequired: double double C = (double)B;

Back to Question 3

Answer 2

A. Compiler Error

Explanation 2

Must cast back to an array type

This program illustrates another very important point. Although you can assign an array object's reference to a reference variable of type Object , you cannot gain access to the elements in the array while treating it as type Object . Instead, you must cast it back to an array type before you can gain access to the elements in the array object.

A portion of the compiler error produced by JDK 1.3 is shown below:

Ap065.java:18: array required, but java.lang.Object found B[0]+ " " + B[1]);

Back to Question 2

Answer 1

C. I'm OK

Explanation 1

Assigning array reference to type Object

This program illustrates a very important point. You can assign an array object's reference to an ordinary reference variable of type Object . Note that I didn't say Object[] . The empty square brackets are not required when the type is Object .

Standard containers or collections

Later on, when we study the various containers in the Java class libraries (see the Java Collections Framework) , we will see that they store references to all objects, including array objects, as type Object . Thus, if it were not possible to store a reference to an array object in a reference variable of type Object , it would not be possible to use the standard containers to store references to array objects.

Because it is possible to assign an array object's reference to a variable of type Object , it is also possible to store array object references in containers of type Object .

Back to Question 1

-end-

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