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