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

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 environments
AI-Robot
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
cell is the building block of life.
Condoleezza Reply
what is cell divisoin?
Aron Reply
Diversity of living thing
ISCONT
what is cell division
Aron Reply
Cell division is the process by which a single cell divides into two or more daughter cells. It is a fundamental process in all living organisms and is essential for growth, development, and reproduction. Cell division can occur through either mitosis or meiosis.
AI-Robot
What is life?
Allison Reply
life is defined as any system capable of performing functions such as eating, metabolizing,excreting,breathing,moving,Growing,reproducing,and responding to external stimuli.
Mohamed
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