如果 long 值小到足以适合 int,是否可以自动将 long 转换为 int?
long i=1;
int j=i; //Error Type mismatch: cannot convert from long to int
最佳答案
long i;
if (i =< Integer.MAX_VALUE && i >= Integer.MIN_VALUE) {
int j = (int) i; // No loss of precision
} else {
throw new Exception("Cannot convert "+i+" to int");
}
关于java - 如果 long 值小到足以适合 int,则 long 转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16923903/