| Author |
ClassCastException Exception
|
Smita Chopra
Ranch Hand
Joined: Jun 27, 2005
Posts: 45
|
|
The following program gives a ClassCastException at runtime Can you please tell me why doesn't it give an error at compile time when its very clear that assigning Object to String is going to fail.
|
 |
Steve Morrow
Ranch Hand
Joined: May 22, 2003
Posts: 657
|
|
|
It may be clear to you, but the compiler doesn't have any way of knowing for certain that such a cast won't work. The example posted is trivial; quite often, a particular implementation is unknown at compile-time, so the compiler trusts you know what you're doing when you cast a reference.
|
 |
Anil Chandran
Greenhorn
Joined: Apr 11, 2005
Posts: 15
|
|
Only at runtime it can be made sure that the object cant be casted to a string . Object is a super class of String . So casting an object to string cant cause a compile time error.
|
I started out with nothing..I still have most of it ...
|
 |
 |
|
|
subject: ClassCastException Exception
|
|
|