<< Chapter < Page Chapter >> Page >

Back to Question 6

Answer 5

B. Runtime Error

Explanation 5


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


Questions & Answers

Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?