Java中最惯用的方法是验证 long
到的转换int
不会丢失任何信息?
这是我当前的实现:
public static int safeLongToInt(long l) {
int i = (int)l;
if ((long)i != l) {
throw new IllegalArgumentException(l + " cannot be cast to int without changing its value.");
}
return i;
}