判等语句不是元子语句,没有同步的话,有可能导致错误的逻辑:if( i != i ){ ... }详细的执行过程如下: 1: mov eax, dword ptr [i] 2: cmp eax, dword ptr [i] 3: je ...当单线程时不会出现逻辑错误,但多线程时就不一定了。假设以上是线程 A 的执行序列,而可能另有线程 B 在执行对 i 进行赋值的序列: (b)1: mov dword ptr[i] 0Ah如果没有对 i 进行同步锁定,则可能出现以下的执行序列,是A、B两个线程的混合: (a)0: mov dword ptr [i] 0h // A 线程设置 i 的值为 0 (a)1: mov eax, dword ptr [i] // A 线程进行 i!=i 的判等计算 ... // 线程调度,切换到 B 线程来执行 (b)1: mov dword ptr [i] 0Ah // B 线程设置 i 的值为 10 ... // 线程调度,切换到 A 线程来执行 (a)2: cmp eax, dword ptr [i] // A 线程进行 i!=i 的后半部分的操作(不是元子操作,当然可能被分成两个阶段执行),取操作数时,得到的是 B 线程设置的值 10,而之前 eax 中的值是 0,cmp 指令比效的两个值分别是 0 和 10, (a)3: je ... // 对 A 来说逻辑已经不正确了。 这时在 A 线程看来,(i!=i) 的值为 true 了。
public class NumberTest { public static void main(String[] args) { test1(); test2(); }
public static void test1() { double i = Double.NaN; if(i != i) { System.out.println("test1"); } } public static void test2() { int i = Integer.MIN_VALUE; if(i != 0 && i == -i){ System.out.println("test2"); } } }第一个,可以看看 Java Language Specification 上的说明节选自 Java Language Specification,第 15.21.1 节:If either operand is NaN, then the result of == is false but the result of != is true. Indeed, the test x!=x is true if and only if the value of x is NaN. (The methods Float.isNaN and Double.isNaN may also be used to test whether a value is NaN.)如果有一个操作数是 NaN,那么“==”的运算结果为 false,而“!=”的结果为 true。如果 x 的 值为 NaN 时,则 x != x 的运算结果是 true。(使用 Float.isNaN 和 Double.isNaN 的方法,可以测 试某个值是否是 NaN。) 参考资料IEEE 754-1985, 5.7 节 http://754r.ucbtest.org/standards/754xml.htmlJava Language Specification,第 4.2.3、15.21.1 节 http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3 http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#5198第二个的话,只要知道正负数是怎么存储的,正负数的范围就能明白了。
火龙果说的对,忘记java中的数值范围了。 Integer.MIN_VALUE : 0x80000000 而 - Integer.MIN_VALUE : 0x80000000 ,所以有 i == -i 而 i != 0佩服!
i != 0 && i == -i : false你说的能成立时指i != i为true还是什么?
if括号里面的是boolean,这两个条件都为false,不会执行if{}里面的
一共3道题 第一题是 if(i1<=i2&&i1>=i2&&i1!=i2){}这个我做出来了
其他2个 我实在想不出来了
if(i != 0 && i == -i){}
是2个不同的程序 没有关联的·不要想在一起哈
int j=i;
//在这里改变j就可以成立了啊
j=2;
if(i!=j){
//成立
}}
从程序的角度讲 这两题是完全可以实现的 但要当数学题做 就没法了
1: mov eax, dword ptr [i]
2: cmp eax, dword ptr [i]
3: je ...当单线程时不会出现逻辑错误,但多线程时就不一定了。假设以上是线程 A 的执行序列,而可能另有线程 B 在执行对 i 进行赋值的序列:
(b)1: mov dword ptr[i] 0Ah如果没有对 i 进行同步锁定,则可能出现以下的执行序列,是A、B两个线程的混合:
(a)0: mov dword ptr [i] 0h // A 线程设置 i 的值为 0
(a)1: mov eax, dword ptr [i] // A 线程进行 i!=i 的判等计算
... // 线程调度,切换到 B 线程来执行
(b)1: mov dword ptr [i] 0Ah // B 线程设置 i 的值为 10
... // 线程调度,切换到 A 线程来执行
(a)2: cmp eax, dword ptr [i] // A 线程进行 i!=i 的后半部分的操作(不是元子操作,当然可能被分成两个阶段执行),取操作数时,得到的是 B 线程设置的值 10,而之前 eax 中的值是 0,cmp 指令比效的两个值分别是 0 和 10,
(a)3: je ... // 对 A 来说逻辑已经不正确了。
这时在 A 线程看来,(i!=i) 的值为 true 了。
test1();
test2();
}
public static void test1() {
double i = Double.NaN;
if(i != i) {
System.out.println("test1");
}
} public static void test2() {
int i = Integer.MIN_VALUE;
if(i != 0 && i == -i){
System.out.println("test2");
}
}
}第一个,可以看看 Java Language Specification 上的说明节选自 Java Language Specification,第 15.21.1 节:If either operand is NaN, then the result of == is false but the result of != is true.
Indeed, the test x!=x is true if and only if the value of x is NaN. (The methods
Float.isNaN and Double.isNaN may also be used to test whether a value is NaN.)如果有一个操作数是 NaN,那么“==”的运算结果为 false,而“!=”的结果为 true。如果 x 的
值为 NaN 时,则 x != x 的运算结果是 true。(使用 Float.isNaN 和 Double.isNaN 的方法,可以测
试某个值是否是 NaN。)
参考资料IEEE 754-1985, 5.7 节
http://754r.ucbtest.org/standards/754xml.htmlJava Language Specification,第 4.2.3、15.21.1 节
http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#5198第二个的话,只要知道正负数是怎么存储的,正负数的范围就能明白了。
Integer.MIN_VALUE : 0x80000000
而 - Integer.MIN_VALUE : 0x80000000 ,所以有 i == -i 而 i != 0佩服!
如:
if(new Date().getTime() != new Date().getTime()){
System.out.println("成立吗??");
}
public class Test { public static void main(String[] args) {
test3();
}
//精度丢失
public static void test3() {
long i = Long.MAX_VALUE;
double k=(double) Long.MAX_VALUE;
long j = Long.MAX_VALUE - 1;
if( j==k) {
System.out.println("test3");
}
}
}