<< Chapter < Page | Chapter >> Page > |
B. Runtime Error
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)
C. 1.0 2.0
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.
A. Compiler Error
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;
A. Compiler Error
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]);
C. I'm OK
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 .
-end-
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?