Why cannot cast Integer to String in java?

后端 未结 11 2475
时光取名叫无心
时光取名叫无心 2020-11-29 18:00

I found some strange exception:

java.lang.ClassCastException: java.lang.Integer 
 cannot be cast to java.lang.String

How it can be possible

相关标签:
11条回答
  • 2020-11-29 18:52

    Use .toString instead like below:

    String myString = myIntegerObject.toString();
    
    0 讨论(0)
  • 2020-11-29 18:55

    You should call myIntegerObject.toString() if you want the string representation.

    0 讨论(0)
  • 2020-11-29 18:58

    In your case don't need casting, you need call toString().

    Integer i = 33;
    String s = i.toString();
    //or
    s = String.valueOf(i);
    //or
    s = "" + i;
    

    Casting. How does it work?

    Given:

    class A {}
    class B extends A {}
    

    (A)
      |
    (B)

    B b = new B(); //no cast
    A a = b;  //upcast with no explicit cast
    a = (A)b; //upcast with an explicit cast
    b = (B)a; //downcast
    

    A and B in the same inheritance tree and we can this:

    a = new A();
    b = (B)a;  // again downcast. Compiles but fails later, at runtime: java.lang.ClassCastException
    

    The compiler must allow things that might possibly work at runtime. However, if the compiler knows with 100% that the cast couldn't possibly work, compilation will fail.
    Given:

    class A {}
    class B1 extends A {}
    class B2 extends A {}
    

            (A)
          /       \
    (B1)       (B2)

    B1 b1 = new B1();
    B2 b2 = (B2)b1; // B1 can't ever be a B2
    

    Error: Inconvertible types B1 and B2. The compiler knows with 100% that the cast couldn't possibly work. But you can cheat the compiler:

    B2 b2 = (B2)(A)b1;
    

    but anyway at runtime:

    Exception in thread "main" java.lang.ClassCastException: B1 cannot be cast to B2

    in your case:

              (Object)
                /       \
    (Integer)       (String)

    Integer i = 33;
    //String s = (String)i; - compiler error
    String s = (String)(Object)i;
    

    at runtime: Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

    0 讨论(0)
  • 2020-11-29 19:00

    Why this is not possible:

    Because String and Integer are not in the same Object hierarchy.

          Object
         /      \
        /        \
    String     Integer
    

    The casting which you are trying, works only if they are in the same hierarchy, e.g.

          Object
         /
        /
       A
      /
     /
    B
    

    In this case, (A) objB or (Object) objB or (Object) objA will work.

    Hence as others have mentioned already, to convert an integer to string use:

    String.valueOf(integer), or Integer.toString(integer) for primitive,

    or

    Integer.toString() for the object.

    0 讨论(0)
  • 2020-11-29 19:03

    Objects can be converted to a string using the toString() method:

    String myString = myIntegerObject.toString();
    

    There is no such rule about casting. For casting to work, the object must actually be of the type you're casting to.

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