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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…