(a!= b)和(a!=(a = b))之间有什么区别? [英] What is the difference between (a != b) and (a != (a = b)?
问题描述
在最近的问题,我们发现了以下代码:
In a recent question, we found the following piece of code:
// p, t, q and tail are Node<E> objects.
p = (p != t && t != (t = tail)) ? t : q;
忽略问题的上下文,我对以下行为感兴趣:
Omitting the context of the question, I am interested in the following behavior:
t != (t = tail)
考虑到这些是相同类型的对象,无论何种类型。这和之间有什么区别:
Considering those are the same type of objects, whatever the type. Is there any difference between this and:
t != tail
或者我在比较机制中缺少一些至关重要的东西?
Or am I missing something crucial in the comparison mechanism ?
编辑
如果有人想知道,可以在 ConcurrentLinkedQueue 类来自 java.util
,第352行。
If anyone wonders, this is found in the ConcurrentLinkedQueue class from java.util
, line 352.
推荐答案
t != (t = tail)
相当于
oldt = t;
t = tail;
... oldt != t...
即。将 t
的原始值与 tail
进行比较,另外 t
被指定为 tail
的值。
i.e. the original value of t
is compared to tail
and in addition t
is assigned the value of tail
.
这是一个简短的写作方式
It's a short way of writing
if (t != tail) {
t = tail;
}
这篇关于(a!= b)和(a!=(a = b))之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!