Object x = new Integer(1218); System.out.println((String)x);
如上代碼編譯經過,運行時報錯:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
getClass().getName()+'@'+Integer.toHexString(hashCode())
java.lang.NullPointerException
public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); }
Integer o = new Integer(1218); System.out.println((String)o);
Cannot cast from Integer to String
,說明Integer不能經過強制轉換成String。Object x = new Integer(1218); System.out.println((String)x);