This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
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..
Joined: Nov 22, 2008
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
Joined: Nov 22, 2008
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.
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.