R
R. Clayton
Thanks everyone for the responses.
Stefan Ram:
int i =( int )doubleValue;
I take this example to be a conversion cast, although I can see how it
could be considered a downcast. My question involved casting with respect
to reference types, where there's no bit-munging conversion going on. I
was sloppy in implicitly assuming that downcasting involves references.
Mike Schilling:
Requiring the explicit cast 1. Ensures that the author of the code realizes
that he's written something that, on the surface, at least, might
fail. 2. Alerts readers of the code of the same thing.
I suppose, but an upcasting assignment can fail too (albeit always at
compile time) if the right-hand side type can't possibly be a descendant
of the left-hand side type. Shouldn't this logic dictate an explicit
upcast too?
Arne Vajhøj:
The last code is a hack that violates good OOP and can potentially
result in a ClassCastException.
You should be happy that they allow the last one !
I'm certainly willing to believe it's bad programming practice, or at least
a code smell. I'm thinking, however, that without up- and downcasting to
and from Objects it wouldn't be possible to implement general-purpose
containers.
Eric Sosman:
Java answers "Okay, boss, whatever you say (but I'll double-check when the
assignment actually occurs, and if the boss is wrong I'll go on strike with
ClassCastException)."
Right, except that Java double-checks the declared type of the assigned-to
variable, which it knows, against the dynamic type of the assigning
variable, which it finds out at run-time. The cast provides no useful
information to the double-check.
Joshua Cranmer:
If you're asking, why require a cast even though it can't prove it,
the simplest reason is that it would be an unsafe cast.
It would be unsafe without the cast, you mean? That's not clear to me. In
the absence of a compile-time proof that the assignment is type correct the
matter is deferred until run-time, but in either case the cast doesn't seem
to be adding any useful information.
Daniel Pitts:
The downcast is an acknowledgment by the programmer that they are sure that
the dynamic type is assignable to the static type. It forces the programmer
to carefully consider if this is indeed true.
Except that downcasting is always required, independent of the programmer's
ability to prove the type correctness of the assignment.
Mark Space:
Making an explicit cast is just easier on the compiler,
That's the question: how does a downcast make things easier? It seems to
add redundant information, the compiler has parse and type-check it, and it
adds another point at which a programmer can make a mistake.
Roedy Green:
http://mindprod.com/jgloss/cast.html
But I don't think that the downcast "assures Java that the dogRef pointer
really points to a Dalmatian.". The run-time check does that in the
absence of successful compile-time type analysis (for which the downcast is
unneeded, as far as I can tell). I was pleased, however, that the page
considers the (int) example above a conversion cast too.
Michael Jung:
In this case it can, but in some expressions an explicit downcast is
needed, e. g.: inherit((Child)p);
Assuming inherit() is a method, the formal parameter has a declared type,
which the compiler knows.
Stefan Ram:
int i =( int )doubleValue;
I take this example to be a conversion cast, although I can see how it
could be considered a downcast. My question involved casting with respect
to reference types, where there's no bit-munging conversion going on. I
was sloppy in implicitly assuming that downcasting involves references.
Mike Schilling:
Requiring the explicit cast 1. Ensures that the author of the code realizes
that he's written something that, on the surface, at least, might
fail. 2. Alerts readers of the code of the same thing.
I suppose, but an upcasting assignment can fail too (albeit always at
compile time) if the right-hand side type can't possibly be a descendant
of the left-hand side type. Shouldn't this logic dictate an explicit
upcast too?
Arne Vajhøj:
The last code is a hack that violates good OOP and can potentially
result in a ClassCastException.
You should be happy that they allow the last one !
I'm certainly willing to believe it's bad programming practice, or at least
a code smell. I'm thinking, however, that without up- and downcasting to
and from Objects it wouldn't be possible to implement general-purpose
containers.
Eric Sosman:
Java answers "Okay, boss, whatever you say (but I'll double-check when the
assignment actually occurs, and if the boss is wrong I'll go on strike with
ClassCastException)."
Right, except that Java double-checks the declared type of the assigned-to
variable, which it knows, against the dynamic type of the assigning
variable, which it finds out at run-time. The cast provides no useful
information to the double-check.
Joshua Cranmer:
If you're asking, why require a cast even though it can't prove it,
the simplest reason is that it would be an unsafe cast.
It would be unsafe without the cast, you mean? That's not clear to me. In
the absence of a compile-time proof that the assignment is type correct the
matter is deferred until run-time, but in either case the cast doesn't seem
to be adding any useful information.
Daniel Pitts:
The downcast is an acknowledgment by the programmer that they are sure that
the dynamic type is assignable to the static type. It forces the programmer
to carefully consider if this is indeed true.
Except that downcasting is always required, independent of the programmer's
ability to prove the type correctness of the assignment.
Mark Space:
Making an explicit cast is just easier on the compiler,
That's the question: how does a downcast make things easier? It seems to
add redundant information, the compiler has parse and type-check it, and it
adds another point at which a programmer can make a mistake.
Roedy Green:
http://mindprod.com/jgloss/cast.html
But I don't think that the downcast "assures Java that the dogRef pointer
really points to a Dalmatian.". The run-time check does that in the
absence of successful compile-time type analysis (for which the downcast is
unneeded, as far as I can tell). I was pleased, however, that the page
considers the (int) example above a conversion cast too.
Michael Jung:
In this case it can, but in some expressions an explicit downcast is
needed, e. g.: inherit((Child)p);
Assuming inherit() is a method, the formal parameter has a declared type,
which the compiler knows.