
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - ClassCastException
The Java ClassCastException is thrown when an object is cast to an incompatible class. The ClassCastException is a runtime exception that is thrown when you try to cast an object to a subclass of which it is not an instance.
There are some scenarios when JVM throws a ClassCastException in Java, two of them are following:
- When the user tries to cast an object to an incompatible class, JVM throws a ClassCastException.
- When the user tries to cast an object to a subclass of which it is not an instance, JVM throws a ClassCastException.
Constructors of ClassCastException
There are two constructors of ClassCastException class:
- ClassCastException(): This constructor is used to create a ClassCastException object without any message.
- ClassCastException(String message): This constructor is used to create a ClassCastException object with a message.
Methods of ClassCastException
There are some methods of ClassCastException class:
Method | Description |
---|---|
getMessage() | It is used to return the message of the exception. |
toString() | It is used to return the detail message string of the exception. |
printStackTrace() | It is used to print the stack trace of the exception. |
Example of ClassCastException
In this example, we are trying to cast an object to an incompatible class, so JVM will throw a ClassCastException.
public class ClassCastExceptionExample { public static void main(String[] args) { Object obj = new Integer(10); String str = (String)obj; } }
Output
Following is the output of the above code:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at ClassCastExceptionExample.main(ClassCastExceptionExample.java:4)
As you can see in the output, JVM throws a ClassCastException because we are trying to cast an object to an incompatible class.
How to handle ClassCastException
In this example, we are trying to cast an object to an incompatible class, so JVM will throw a ClassCastException. We are handling this exception using a try-catch block.
public class ClassCastExceptionExample { public static void main(String[] args) { Object obj = new Integer(10); try { String str = (String)obj; } catch(ClassCastException e) { System.out.println("Can't cast an object to an incompatible class"); } } }
Output
Following is the output of the above code:
Can't cast an object to an incompatible class
How to avoid ClassCastException?
Here are some ways to avoid ClassCastException:
- Always check the type of the object before casting it.
- Always use the instanceof operator before casting an object.
- Always use the getClass() method to check the type of the object before casting it.
These are some ways to avoid ClassCastException in Java.
Let's see an example to avoid ClassCastException:
Example of avoiding ClassCastException
public class ClassCastExceptionExample { public static void main(String[] args) { Object obj = new Integer(10); if(obj instanceof String) { String str = (String)obj; } else { System.out.println("Can't cast an object to an incompatible class"); } } }
Output
Following is the output of the above code:
Can't cast an object to an incompatible class
As you can see in the output, we are avoiding the ClassCastException using the instanceof operator.