Why cannot cast Integer to String in java?

后端 未结 11 2610
时光取名叫无心
时光取名叫无心 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 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.

提交回复
热议问题