• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help in casting topic

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Rupak Khurana and Nicholas Cheung.
 
look! it's a bird! it's a plane! It's .... a teeny tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic