Why cannot cast Integer to String in java?

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

    No. Every object can be casted to an java.lang.Object, not a String. If you want a string representation of whatever object, you have to invoke the toString() method; this is not the same as casting the object to a String.

提交回复
热议问题