• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Casting Object to Object Array and vice versa

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to Suresh.
This is from JLS 10.8
The direct superclass of an array type is Object.
 
They worship nothing. They say it's because nothing lasts forever. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic