在C和C++中,根据Shafik's post,INT_MIN % -1
的行为似乎是未定义/平台相关的。Java的%运算符是否溢出?
在Java中,%运算符是否溢出?
考虑这段代码:
public class Test {
public static void main(String[] args) {
// setup variables:
byte b = Byte.MIN_VALUE % (-1);
short s = Short.MIN_VALUE % (-1);
int i = Integer.MIN_VALUE % (-1);
long l = Long.MIN_VALUE % (-1);
// my machine prints "0" for all:
System.out.println(b);
System.out.println(s);
System.out.println(i);
System.out.println(l);
}
}
是否有一个独立于平台的保证,上述结果是0
?
Java本质上是平台独立的,所以我猜(没选中)答案是YES – shlomi33 2014-09-10 12:28:38