Cannot cast from Object to int

Cannot cast from Object to ** jdk1.7问题

Object[] objs = new Object[5];
objs[0] = 1;
objs[1] = true;
int value = (int)objs[0]; // 会报错
boolean flag = (boolean)objs[1]; //会报错
int value = (Integer)objs[0]; // 换成包装类型即可
boolean flag = (Boolean)objs[1]; //换成包装类型即可

综上所述:jdk1.7之前,大类型强制转小类型时,基本数据类型需要使用包装类。

上一篇:Property or field ‘totalPages‘ cannot be found on null


下一篇:Cannot uninstall ‘wrapt‘ 树莓派安装tensorflow