溢出问题出现
public class Text {
public static void main(String[] args) {
int ms = 24 * 60 * 60 * 1000;
int ps = 24 * 60 * 60 * 1000 *1000;
System.out.println(ps/ms);
}
}
结果 是 5
很显然已经是因为超出 int 的范围 导致的
难道是 没有定义long 导致的吗
public class Text {
public static void main(String[] args) {
long ms = 24 * 60 * 60 * 1000;
long ps = 24 * 60 * 60 * 1000 *1000;
System.out.println(ps/ms);
}
}
结果依旧 是 5
很显然依旧是因为超出 int 的范围 导致的
计算 算式本身 也会超出默认的int
public class Text {
public static void main(String[] args) {
long ms = 24 * 60 * 60 * 1000L;
long ps = 24 * 60 * 60 * 1000 *1000L;
System.out.println(ps/ms);
}
}
1000L 设置之后才能 保证运算的时候就是long 而不是 运算之后的储存出现的错误