ClassCast Exception in Java
In Java, a ClassCastException is a runtime exception that occurs when a program attempts to cast an object to a subclass of which it is not an instance. This exception is thrown when the Java Virtual Machine (JVM) cannot cast an object to the specified type at runtime.
The ClassCastException is a subclass of the RuntimeException class and is typically caused by an error in the code. It usually occurs when the programmer tries to cast an object of one class to another class that is not compatible with it.
The ClassCastException can also occur in situations where the programmer is not explicitly casting objects. For example, when using Java Generics, it is possible to cast a generic type to an incompatible type, which can result in a ClassCastException at runtime.
Algorithm for handling a ClassCastException:
Step 1: Identify the section of code where the ClassCastException is occurring.
Step 2: Surround that section of code with a try-catch block.
Step 3: In the catch block, catch the ClassCastException and handle it appropriately. This could include logging an error message, notifying the user of the error, or taking corrective action to prevent the exception from causing further problems.
Step 4: If necessary, modify the code that caused the ClassCastException to occur so that it correctly casts objects to the appropriate type.
Example code 1:
FileName: Example.java
public class Example {
public static void main(String[] args) {
Object obj = "Hello";
Integer i = (Integer) obj; // Attempting to cast a String to Integer
// The following code will not be executed if the ClassCastException occurs
System.out.println("Casting successful");
}
}
Output:
Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer (java.lang.String and java.lang.Integer are in module java.base of loader 'bootstrap')
at Example.main(Example.java:4)
Handling ClassCastException using try-catch
block:
FileName: Example.java
public class Example {
public static void main(String[] args) {
Object obj = "Hello";
try {
Integer i = (Integer) obj; // Attempting to cast a String to Integer
System.out.println("Casting successful");
} catch (ClassCastException e) {
System.out.println("Object cannot be cast to Integer");
}
}
}
Output:
Object cannot be cast to Integer
Handling ClassCastException using instanceof
operator:
FileName: Example.java
public class Example {
public static void main(String[] args) {
Object obj = "Hello";
if (obj instanceof Integer) {
Integer i = (Integer) obj;
System.out.println("Casting successful");
} else {
System.out.println("Object cannot be cast to Integer");
}
}
}
Output:
Object cannot be cast to Integer
Example code 2:
FileName: Main.java
public class Main {
public static void main(String[] args) {
// creating an array of objects with different types
Object[] objects = new Object[3];
objects[0] = new Integer(10);
objects[1] = new String("Hello");
objects[2] = new Double(3.14);
// casting the objects to incorrect types
for (Object obj : objects) {
// will throw ClassCastException at runtime
Integer intValue = (Integer) obj;
System.out.println(intValue);
}
}
}
Output:
10
Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer
(java.lang.String and java.lang.Integer are in module java.base of loader 'bootstrap')
at Main.main(Main.java:12)
Handling ClassCastException using try-catch
block:
FileName: Main.java
public class Main {
public static void main(String[] args) {
Object[] objects = new Object[3];
objects[0] = new Integer(10);
objects[1] = new String("Hello");
objects[2] = new Double(3.14);
for (Object obj : objects) {
try {
if (obj instanceof Integer) {
Integer intValue = (Integer) obj;
System.out.println(intValue);
} else if (obj instanceof String) {
String strValue = (String) obj;
System.out.println(strValue);
} else if (obj instanceof Double) {
Double dblValue = (Double) obj;
System.out.println(dblValue);
} else {
System.out.println("Unknown object type");
}
} catch (ClassCastException e) {
System.err.println("Caught ClassCastException: " + e.getMessage());
e.printStackTrace();
}
}
}
}
Output:
10
Hello
3.14
Handling ClassCastException using instanceof
operator:
FileName: Main.java
public class Main {
public static void main(String[] args) {
Object[] objects = new Object[3];
objects[0] = new Integer(10);
objects[1] = new String("Hello");
objects[2] = new Double(3.14);
for (Object obj : objects) {
if (obj instanceof Integer) {
Integer intValue = (Integer) obj;
System.out.println(intValue);
} else if (obj instanceof String) {
String strValue = (String) obj;
System.out.println(strValue);
} else if (obj instanceof Double) {
Double dblValue = (Double) obj;
System.out.println(dblValue);
} else {
System.out.println("Unknown object type");
}
}
}
}
Output:
10
Hello
3.14