• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting Object to Object Array and vice versa  RSS feed

 
Shailesh Misra
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I observed that Casting Object to Object Array and vice versa does not throw a ClassCastException. I was expecting a runtime error at 1.
Please help,
_____________________________________________________________
class test
{
public static void main(String[] args)
{
abc[] i = new abc[2];
Object o = new Object();
o=i;
i=(abc[])o; // I was expecting error here at runtime.
}
}
class abc{int a=1;}
_____________________________________________________________
regards,
Shailesh

[This message has been edited by Shailesh Misra (edited October 16, 2001).]
 
Marilyn de Queiroz
Sheriff
Posts: 9081
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shailesh Misra:
I observed that Casting Object to Object Array and vice versa does not throw a ClassCastException. I was expecting a runtime error at 1.
Please help,
_____________________________________________________________
<pre>class test
{
public static void main(String[] args)
{
abc[] i = new abc[2];
Object o = new Object();
o = i;
i = (abc[])o ; // I was expectime error here at runtime.
}
}

class abc
{
int a = 1;
}
</pre>_____________________________________________________________
regards,
Shailesh

Why were you expecting a runtime error when you are casting back to the original type? Anything can be cast to an Object, even an array. If you cast the Object back to something else, ... for instance an int[] array rather than an abc[] array, ... you will get a compile time error.

[This message has been edited by Marilyn deQueiroz (edited October 17, 2001).]
 
suresh seeram
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shailesh,
abc[] i = new abc[2];
Object o = new Object();
o=i;//1
i=(abc[])o;//2

It will compile and run properly, it does not throw any exception..
Hierarchy is like this..
Object[] is subclass of Object.
abc[] is subclass of Object[] (Object array)..
so abc[] indirectly subclass of Object.. ok..
o=i;//1.Here implicit cast takes place. becasue i(arr[]) is subclass of o(Object).
i=(abc[])o;//2.It will compile properly.. At runtime it will check that whethr o is of type abc[] or not.. here o referes to abc[] type so there is not runtime exception..
IF you remove the statement 1 in your code.. you will get runtime exception..
I hope this will help you..
Correct me if i am wrong..
regards,
Suresh
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shailesh, just comment this line
and see the difference.
//o=i;
--Farooq
 
Shailesh Misra
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
I got it. In Heller i saw the table that says :
If old type is a non final class and
new type is reference to an any object array type say abc[] type
and we cast as follows
nt=(newtype)ot;
Then this will compile only if - "newtype must be object". I think that oldtype must be an object. new type is an array.
regards,

 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to Suresh.
This is from JLS 10.8
The direct superclass of an array type is Object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!