aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Need help in casting topic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Need help in casting topic" Watch "Need help in casting topic" New topic
Author

Need help in casting topic

Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
Hi,
In the programmer's guide to java certification book i found this question:


Given the following program, which statement is true?
public class MyClass {
public static void main(String[] args) {
A[] arrA;
B[] arrB;

arrA = new A[10];
arrB = new B[20];
arrA = arrB; // (1)
arrB = (B[]) arrA; // (2)
arrA = new A[10];
arrB = (B[]) arrA; // (3)
}
}

class A {}

class B extends A {}

Select the one correct answer.

a.The program will fail to compile because of the assignment at (1).

b.The program will throw a java.lang.ClassCastException in the assignment at (2) when run.

c.The program will throw a java.lang.ClassCastException in the assignment at (3) when run.

d.The program will compile and run without errors, even if the (B[]) cast in the statements at (2) and (3) is removed.

e.The program will compile and run without errors, but will not do so if the (B[]) cast in statements at (2) and (3) is removed

The answer was c.
why? please help.

Thank you.


SCWCD 1.4(Loading...), SCJP 1.4(98%), Bachelor of Engineering (computer science)
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
An array itself is an Object, and thus, you try to cast an Object as B[] will encounter ClassCastException.

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Rupak Khurana
Ranch Hand

Joined: Mar 01, 2005
Posts: 89
For the same reason as in the following code snippet (without arrays) :-

A myA;
B myB;
myA = new A();
myB = (B)myA; // ClassCastException is thrown here

Compile Time Rule :- When oldType and newType are classes, one class must be the subclass of another, no matter which one.. For this reason, the compilation succeeds in above code.

Run Time Rule :- When newType is a class, the class of the expression being converted must be newType or must inherit from newType.. Here the class of the expression being converted is A which is neither B nor inherits from B. Therefore the ClassCastException at run time.


Similarly, for Arrays :-

Compile Time Rule :- When both oldType and newType are arrays, both arrays must contain reference types (not primitives) and an element of one type must be same or subclass of the element of another type, no matter which one.

Run Time Rule :- The element of the type being converted (oldType..i.e., the actual object type it refers to) must be same as or subclass of the element of the newType otherwise ClassCastException

HTH


SCJP 1.5<br />SCWCD 1.4
Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
Thank you Rupak Khurana and Nicholas Cheung.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help in casting topic
 
Similar Threads
Reference Casting
Arrays
Question on Casting
ClassCastException??
Ques on casting referances ...