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