permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting of Arrays and Object 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 "Casting of Arrays and Object" Watch "Casting of Arrays and Object" New topic
Author

Casting of Arrays and Object

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
Anand Iyer
Greenhorn

Joined: Jun 13, 2000
Posts: 26
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..
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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,
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
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.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
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 Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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'm not back." - Bill Harding, Twister
pardha
Greenhorn

Joined: Jun 18, 2000
Posts: 11
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.

zhewen
Ranch Hand

Joined: Jul 18, 2000
Posts: 35
Can you pl. tell me what is difference hier, when
o declared with:
static Object o;
//or
o=new Object();

i think the both is referenced with a value of null Object.
but we get such different results.

regds
zhewen
Indu mathy
Greenhorn

Joined: Jun 08, 2000
Posts: 8
Originally posted by Carl Trusiak:
[B]
[/B]

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


Indu
Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
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[].
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Casting of Arrays and Object
 
Similar Threads
storage
Method calling
exam lab says "volatile can used to modify only instance var"
Array of Objects
var-args