I was reading about generics and type safety and found that arrays cannot be generic in java. I also understood the covariant nature of arrays which directed me to the array store exception. I understand why this exception occurs.
I tried the below code
class SuperClass {
}
class SubClass extends SuperClass {
}
public class ArrayCheck {
public static void main (String args[]) {
SubClass arr[] = new SubClass[10];
arr[0] = new SubClass();
SuperClass[] arr1 = arr;
arr1[1] = new SuperClass();
}
}
This gives an ArrayStoreException as expected. My questions are
My question is how does JVM check for array types at runtime? Does the compiler append any additional code or is there some predefined process that JVM follows before executing instructions?
At what point this exception was raised? I thought that the
ArrayStoreException
would only occur if I try to read the array but I was wrong. So I am not understanding at what point exactly this error was raised.
Also, need a bit of clarification on the Java program execution process.
- Are run time errors and exceptions only the errors that occur while the program is in execution i.e JVM has started interpreting and executing the instructions or an error during bytecode verification is also considered run time error.