aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ArrayStoreException pls explain? 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 "ArrayStoreException pls explain?" Watch "ArrayStoreException pls explain?" New topic
Author

ArrayStoreException pls explain?

vikram choudhary .
Greenhorn

Joined: Apr 25, 2007
Posts: 7
import java.io.Serializable;
class Animal{

}
class Dog extends Animal{

}
class NewClass {
public static void main(String... a) {

Dog[] dogs=new Dog[10];
Animal[] animals=new Animal[10];
animals=dogs;
animals[0]=new Animal(); // ArrayStoreException
Animal m=new Dog();
m=new Animal();// No Exception


}
}

pls explain?
Srikanth CS
Greenhorn

Joined: Apr 10, 2007
Posts: 9
Original code :
1.import java.io.Serializable;
2.class Animal{

3.}
4.class Dog extends Animal{

4.}
5.class NewClass {
6.public static void main(String... a) {

7.Dog[] dogs=new Dog[10];
8.Animal[] animals=new Animal[10];
9.animals=dogs;
10.animals[0]=new Animal();
11.System.out.println(animals[0]);
12.Animal m=new Dog();
13.m=new Animal();
}
}

The exception is due to line no 9.
if you make line no 9. a comment line the program will compile without errors.
Other wise at line no 10. if you call the Dog class constructor that is
animals[0] = new Dog();
the program will compile without error.

Thus assiging the reference typr dog to animals without inhertance has caused the error.


HOpe i am right......
Srikanth CS
Greenhorn

Joined: Apr 10, 2007
Posts: 9
Original code :
1.import java.io.Serializable;
2.class Animal{

3.}
4.class Dog extends Animal{

4.}
5.class NewClass {
6.public static void main(String... a) {

7.Dog[] dogs=new Dog[10];
8.Animal[] animals=new Animal[10];
9.animals=dogs;
10.animals[0]=new Animal();
11.System.out.println(animals[0]);
12.Animal m=new Dog();
13.m=new Animal();
}
}

The exception is due to line no 9.
if you make line no 9. a comment line the program will compile without errors.
Other wise at line no 10. if you call the Dog class constructor that is
animals[0] = new Dog();
the program will compile without error.

Thus assiging the reference type dog to animals caused the error.


Hope i am right......
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Vikram,



Line 1: It is our preparation step to make the compiler fool before Line 1
Dog IS-A(n) Animal
So Animal reference variable (reference to one D array) can simply refer
to its subclass (reference of one D array).
Runtime type of the ani would be Dog.
Just check it out:

After Line 1 place this line:


Compiler could not recognize the Line 2, because it thinks as:
ani[0] can hold reference of the Animal object. Because it sees that
ani is reference varaible of type Animal;

But at the runtime when you try to store Animal object ani[0] ArrayStoreException occurs because it can't fit the parent to the child.





Regards,
cmbhatt


cmbhatt
 
Don't get me started about those stupid light bulbs.
 
subject: ArrayStoreException pls explain?