This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help in casting topic

 
Adil El mouden
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An array itself is an Object, and thus, you try to cast an Object as B[] will encounter ClassCastException.

Nick
 
Rupak Khurana
Ranch Hand
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Adil El mouden
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rupak Khurana and Nicholas Cheung.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic