public class Abc { static Object o; public static void main(String[]args) { int[]n=(int[])o; } } If someone can explain why the above code compiles and runs
I think its bcos.. Array is a object..so extends the Object class If you are assigning a superclass reference to a subclass, then you need to cast it to the subclass type and thats what you have done in the code.. Hope it helps..
Hi, Adding on to what Anand had said : "You can assign an array to an object without casting => instance of an Object can refer to array object. Therefore it should be possible to cast an object to array". Hope it helps. Cheers Sankar
Hi, sonali; IMHO, your program will compile, but will throw a runtime exception ClassCastException because although o of type Object and array is also subtype of Object( you can get by with explicit casting), at runtime o, of type Object can not represent Array object. Correct me if I am wrong. Regards,
The only reason it doesn't throw a class cast exception is that the value of o is null. If o had been initialized with: o = new Object(); then a class cast exception would be thrown. The compiler assumes you know what you are doing with the(int[]) cast, without that you would get a compiler error.
Originally posted by William Brogden: The only reason it doesn't throw a class cast exception is that the value of o is null. If o had been initialized with: o = new Object(); then a class cast exception would be thrown. The compiler assumes you know what you are doing with the(int[]) cast, without that you would get a compiler error.
This compiles and runs! Additionally try the following example and tell me what you get for an output.
I get exactly the results William described: if o is initialized with <code>o = new Object();</code> there is a ClassCaseException at runtime. If the class cast <code>(int[])</code> is then dropped, the code doesn't compile. As for your additional code, it produces the output "a Reference is null.", as expected. I'm not sure what your point is with this code - it verifies that a class variable is automatically initialized to null if not explicitly initialized to something else. Which was part of William's point, right? It also shows that a reference can be cast to a superclass of the original type, which is always true whether the reference is null or not.
I guess(being very recent to Java), o is an Object of Object class and which is super class. int[] - Any array is an object in Java which is a subclass of object.Here int[] became temporary object ,so we can cast like this (int[])o . Correct me ,If I am wrong.
ur correct but in tha above code u can have only one public class May be u have written all the class as public by mistake. if we say A a =new B() then a ia not null Thanx
null can be cast to any reference type. For eg try this: System.out.println((Integer)null); It will print "null". Thus the point that Bill is trying to make is that since o contains null, it can be cast to int[]. However if we had Object o = new Object(), then the code would compile (since arrays have Object as superclass), but it would throw a ClassCastException at run-time since o does not actually contain a reference to int[].
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
Gift giving made easy with the permaculture playing cards