在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;
}