Java – ClassCastException

ClassCastException: Thrown when an object is cast to a subclass type that it is not an instance of.
Example:

Object obj = new String("Hello World");
try {
Integer number = (Integer) obj;
} catch (ClassCastException e) {
System.out.println("Caught ClassCastException: " + e.getMessage());
}

No images available.