i == (i = 2) 的结果是什么?
2022-09-01 00:57:08
						以下代码如下:
// In Java, output #####
public static void main(String[] args) {
    int i = 1;
    if(i == (i = 2)) {
        System.out.println("@@@@@");
    } else {
        System.out.println("#####");
    }
}
但:
// In C, output @@@@@,I did test on Clion(GCC 7.3) and Visual Studio 2017
int main(int argc, char *argv[]) {
    int i = 1;
    if(i == (i = 2)) {
        printf("@@@@@");
    } else {
        printf("#####");
    }
    return 0;
}
提出这个问题的动机来自以下代码:
// The code is from the JDK 11 - java.util.concurrent.atomic.AtomicInteger
// I am curious about the behavior of the variable prev.
public final int getAndUpdate(IntUnaryOperator updateFunction) {
    int prev = get(), next = 0;
    for (boolean haveNext = false;;) {
        if (!haveNext)
            next = updateFunction.applyAsInt(prev);
        if (weakCompareAndSetVolatile(prev, next))
            return prev;
        haveNext = (prev == (prev = get()));
    }
}
那么,如何解释以上两种不同的执行模式呢?
 
					 
				 
				    		 
				    		 
				    		 
				    		