At first, excuse me if this question seems stupid. Actually, I am new to Java programing. I have the following code:
public class Test{
public static void main(String[] args){
Object[] ob = new Object[2];
ob[0] = new Integer("1");
ob[1] = new Integer("2");
Integer[] o = (Integer)ob;
System.out.println(o.length());
}
}
when compiling this code a classCastException
exception is thrown. Why?. I know that Object
type can not be cast to Integer
type. But, in fact, each element of the array ob
is an instance of Integer
class which means the casting is logically true. Am I mistaken?.
Thanks in advance.