Implicit boost and explicit downcasting in java

When java can implicitly cast, why does it implicitly list? Please explain with a simple example?

+6
source share
1 answer

The fact is that acceleration will always be successful, therefore it is safe, while downcasting can fail:

String x = getStringFromSomewhere(); Object y = x; // This will *always* work 

But:

 Object x = getObjectFromSomewhere(); String y = (String) x; // This might fail with an exception 

Since this is a "dangerous" operation, the language forces you to do this explicitly - you basically tell the compiler, "I know more than you do at this moment!"

+9
source

All Articles