What is a Java ClassCastException?

The ClassCastException thrown to indicate that your code has attempted to cast an object to a subclass of which it is not an instance. example
public class sample { public static void main(String[] args) { Object i = new Integer(0); System.out.println((String)i); } }
output
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer canno t be cast to java.lang.String at sample.main(sample.java:5)
In the above example, when tries to cast an Integer to a String , String is not a subclass of Integer, so a ClassCastException will be thrown. So, this exaception occurs when you try to cast an instance of an Object to a type that it is not. Casting only works when the casted object follows an is a relationship to the type you are trying to cast to.

Exception hierarchy

All Java errors implement the java.lang.Throwable interface, or are extended from another inherited class therein. The full exception hierarchy of this error is:
java ClassCastException
The ClassCastException extends the RuntimeException class and thus, belongs to those exceptions that can be thrown during the operation of the JVM (Java Virtual Machine). It is an unchecked exception and thus, it does not need to be declared in a method's or a constructor's throws clause.

When will be ClassCastException is thrown:

  1. When you try to cast an object of Parent class to its Child class type, this exception will be thrown.

  2. When you try to cast an object of one class into another class type that has not extended the other class or they don't have any relationship between them.

Casting in Java

All casting in Java really means is taking an Object of one particular type and turning it into another Object type. This process is called casting a variable .

Upcasting and Downcasting in Java

Upcasting is casting to a supertype , while downcasting is casting to a subtype . Upcasting is always allowed, but downcasting involves a type check and can throw a ClassCastException.

Here are some basic rules to keep in mind when casting variables:

  1. Casting an object from a sub class to a super class doesn't require an explicit cast.

  2. Casting an object from a super class to a sub class requires an explicit cast.

  3. The compiler will not allow casts to unrelated types.
Even when the code compiles without issue, an exception may be thrown at run time if the object being cast is not actually an instance of that class . This will result in the run time exception ClassCastException .

Downsides of Casting


Downsides of Casting in Java
There is a certain amount of risk that goes along with downcasting your variables. If you were to try to cast something like an Integer to a String, then you'll get a ClassCastException . This is what's known as a run-time exception , as it's really only detectable when your program is running. So, unless you're doing something with error handling , then your program will likely exit or you'll get an ugly error message on your webpage. So just make sure that if you are doing any downcasting , that you're well aware of the type of object you'll be casting.