VOOZH about

URL: https://www.geeksforgeeks.org/java/how-to-fix-java-lang-classcastexception-in-java/

⇱ How to Fix java.lang.classcastexception in Java? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Fix java.lang.classcastexception in Java?

Last Updated : 12 Dec, 2022

ClassCastException in Java occurs when we try to convert the data type of entry into another. This is related to the type conversion feature and data type conversion is successful only where a class extends a parent class and the child class is cast to its parent class. 

Here we can consider parent class as vehicle and child class may be car, bike, cycle etc., Parent class as shape and child class may be 2d shapes or 3d shapes, etc.

Two different kinds of constructors available for ClassCastException.

  1. ClassCastException(): It is used to create an instance of the ClassCastException class.
  2. ClassCastException(String s): It is used to create an instance of the ClassCastException class, by accepting the specified string as a message.

Let us see in details


Output
10000000.4499999992549419403076171875

If we try to print this value by casting to different data type like String or Integer etc., we will be getting ClassCastException. 

Output :

Exception in thread "main" java.lang.ClassCastException: class java.math.BigDecimal cannot be cast to class java.lang.String (java.math.BigDecimal and java.lang.String are in module java.base of loader 'bootstrap')

at Main.main(Main.java:11)

We can fix the exception printing by means of converting the code in the below format:


Output
10000000.4499999992549419403076171875

So, in any instances when we try to convert data type of object, we cannot directly downcast or upcast to a specified data type. Direct casting will not work and it throws ClassCastException. Instead, we can use

String.valueOf() method. It converts different types of values like int, long, boolean, character, float etc., into the string. 

  1. public static String valueOf(boolean boolValue)
  2. public static String valueOf(char charValue)
  3. public static String valueOf(char[] charArrayValue)
  4. public static String valueOf(int intValue)
  5. public static String valueOf(long longValue)
  6. public static String valueOf(float floatValue)
  7. public static String valueOf(double doubleValue)
  8. public static String valueOf(Object objectValue)

are the different methods available and in our above example, the last method is used.

Between Parent and Child class. Example shows that the instance of the parent class cannot be cast to an instance of the child class.


Compiler error :

👁 Compile time error occurrence when tried to convert parent object to child object

In order to overcome Compile-time errors, we need to downcast explicitly. i.e. downcasting means the typecasting of a parent object to a child object. That means features of parent object lost and hence there is no implicit downcasting possible and hence we need to do explicitly as below way

Giving the snippet where the change requires. In the downcasting Explicit way


Output
BMW
1000000.0
From bike class1000000.0

Upcasting implicit way

An example for upcasting of the child object to the parent object. It can be done implicitly. This facility gives us the flexibility to access the parent class members.


Output
Harley-Davidson
From bike class...0.0

Fixing ClassCastException in an upcasting way and at the same time, loss of data also occurs


Output
An example instance of the Vehicle class to proceed for showing ClassCast Exception
An example instance of the Vehicle class to proceed for showing ClassCast Exception
An example instance of the Bike class that extends 
Vehicle as parent class to proceed for showing ClassCast Exception
After upcasting bike(child) to vehicle(parent)..Bike class display method

Conclusion: By means of either upcasting or downcasting, we can overcome ClassCastException if we are following Parent-child relationships. String.valueOf() methods help to convert the different data type to String and in that way also we can overcome.

Comment
Article Tags:
Article Tags: