Explanation of “ClassCastException” in Java

前端 未结 12 1663
小蘑菇
小蘑菇 2020-11-21 11:12

I read some articles written on \"ClassCastException\", but I couldn\'t get a good idea on that. Is there a good article or what would be a brief explanation?

相关标签:
12条回答
  • 2020-11-21 11:51

    Consider an example,

    class Animal {
        public void eat(String str) {
            System.out.println("Eating for grass");
        }
    }
    
    class Goat extends Animal {
        public void eat(String str) {
            System.out.println("blank");
        }
    }
    
    class Another extends Goat{
      public void eat(String str) {
            System.out.println("another");
      }
    }
    
    public class InheritanceSample {
        public static void main(String[] args) {
            Animal a = new Animal();
            Another t5 = (Another) new Goat();
        }
    }
    

    At Another t5 = (Another) new Goat(): you will get a ClassCastException because you cannot create an instance of the Another class using Goat.

    Note: The conversion is valid only in cases where a class extends a parent class and the child class is casted to its parent class.

    How to deal with the ClassCastException:

    1. Be careful when trying to cast an object of a class into another class. Ensure that the new type belongs to one of its parent classes.
    2. You can prevent the ClassCastException by using Generics, because Generics provide compile time checks and can be used to develop type-safe applications.

    Source of the Note and the Rest

    0 讨论(0)
  • 2020-11-21 11:51

    Exception is not a subclass of RuntimeException -> ClassCastException

        final Object  exception = new Exception();
        final Exception data = (RuntimeException)exception ;
        System.out.println(data);
    
    0 讨论(0)
  • 2020-11-21 11:55

    Do you understand the concept of casting? Casting is the process of type conversion, which is in Java very common because its a statically typed language. Some examples:

    Cast the String "1" to an int -> no problem

    Cast the String "abc" to an int -> raises a ClassCastException

    Or think of a class diagram with Animal.class, Dog.class and Cat.class

    Animal a = new Dog();
    Dog d = (Dog) a; // No problem, the type animal can be casted to a dog, because its a dog.
    Cat c = (Dog) a; // Raises class cast exception; you can't cast a dog to a cat.
    
    0 讨论(0)
  • 2020-11-21 12:02

    You are trying to treat an object as an instance of a class that it is not. It's roughly analogous to trying to press the damper pedal on a guitar (pianos have damper pedals, guitars don't).

    0 讨论(0)
  • 2020-11-21 12:05

    A class cast exception is thrown by Java when you try to cast an Object of one data type to another.

    Java allows us to cast variables of one type to another as long as the casting happens between compatible data types.

    For example you can cast a String as an Object and similarly an Object that contains String values can be cast to a String.

    Example

    Let us assume we have an HashMap that holds a number of ArrayList objects.

    If we write code like this:

    String obj = (String) hmp.get(key);
    

    it would throw a class cast exception, because the value returned by the get method of the hash map would be an Array list, but we are trying to cast it to a String. This would cause the exception.

    0 讨论(0)
  • 2020-11-21 12:10

    A Java ClassCastException is an Exception that can occur when you try to improperly convert a class from one type to another.

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    public class ClassCastExceptionExample {
    
      public ClassCastExceptionExample() {
    
        List list = new ArrayList();
        list.add("one");
        list.add("two");
        Iterator it = list.iterator();
        while (it.hasNext()) {
            // intentionally throw a ClassCastException by trying to cast a String to an
            // Integer (technically this is casting an Object to an Integer, where the Object 
            // is really a reference to a String:
            Integer i = (Integer)it.next();
        }
      }
     public static void main(String[] args) {
      new ClassCastExceptionExample();
     }
    }
    

    If you try to run this Java program you’ll see that it will throw the following ClassCastException:

    Exception in thread "main" java.lang.ClassCastException: java.lang.String
    at ClassCastExceptionExample  (ClassCastExceptionExample.java:15)
    at ClassCastExceptionExample.main  (ClassCastExceptionExample.java:19)
    

    The reason an exception is thrown here is that when I’m creating my list object, the object I store in the list is the String “one,” but then later when I try to get this object out I intentionally make a mistake by trying to cast it to an Integer. Because a String cannot be directly cast to an Integer — an Integer is not a type of String — a ClassCastException is thrown.

    0 讨论(0)
提交回复
热议问题