• 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

Confusion re casting

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy gang..
am currently having masses of confusion over rules regarding casting. I've read various Certification books' approaches to this topic, and have also coded up a few little test classes, but I'm still all of a muddle. Does anyone know of a good, succinct web resource that covers this topic? My main area of trouble is regarding what will compile versus what will actually run.. sorry for bugging y'all, but I can't seem to get my head round this at all!
Thanking you!
Binkie
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I realise that what you're after is some sort of literature, but I figured that I might as well give you a quick answer to your other question, to give you something to start with. The question was:
casting, what will compile and what will run?
The first part is the easy one: It will only compile if the compiler can find a way to do what you ask.
The simple answer to the second one is: It will cause runtime error if you "trick" the compiler.

Hopefully this will give you a start.
/Mike
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I too am having trouble with casting.
I have RHE book and refer to table on p121:
it says to cast an array(Old-type) to a non-final class(New-type), Old-type must be object. Is this backwards? Shouldn't it be new type should be object. Similarly casting a non-final class (old-type) to an array (new-type) it says new-type should be object, this seems backward to.
Also why can I auto-convert an array to Cloneable or Serializable interface, yet casting an array to an interface is always error? why not:
Serializable g = (Serializable) someArray;
or is this table supposed to outline only those situations where cast is necessary and not those situations where cast is redundant?
cheers.

 
Mikael Jonasson
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think it should be. If the casting is to a non-final class, then new typ by definition is an objects (or rather class), the old type must be an object too (or if you prefer: the array must consist of classes):

/Mike
 
Binkie Hayes
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike
thanks a million for this - I've run through your example and it really helped. I'd still really like to see a clear and simple breakdown of the rules.. anyone know any good references?
many thanks to you for your help and support
 
Richard Agnes
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a little confused still.
in your code you are casting the array 'element' to a MyClass object, I am presuming this would obey the normal rules for casting between classes.
I think the book is talking about the casting of an entire array to a single non-final class identifier. I may be corrected here.
So in the case of your code it would appear:
SuperClass d[] = new SuperClass[10];
MyClass e = (MyClass) d ; //I presume the 'a' was a typo
just to clarify my point, the other row in the table specifies that to cast an array (old-type) to an array (new-type) the elements must be type castable, this suggests the original element is indeed an entire array as this rule makes sense under those conditions.
here's more code to illustrate my second point about casting of interfaces, which according to the book an interface (Old-type) can never be cast to an array (new-type):
code:
void test()
{
Object o[]=new Object[10];
Serializable b=o; //b is an interface reference
o=(Object[])b; // this line compiles.
}
help appreciated.
 
reply
    Bookmark Topic Watch Topic
  • New Topic