Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Casting Object to Object Array and vice versa  RSS feed

 
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).]
 
Sheriff
Posts: 9087
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).]
 
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
 
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,

 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!