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) 

Given that these are objects of the same type, regardless of type. Is there any difference between this and:

 t != tail 

Or am I missing something important in the comparison mechanism?


EDIT

If someone wonders, this is in the ConcurrentLinkedQueue class from java.util , line 352.

+4
source share
3 answers
 t != (t = tail) 

equivalently

 oldt = t; t = tail; ... oldt != t... 

i.e. the initial value of t compared with tail and, in addition, t assigned the value of tail .

This is a short way to record.

 if (t != tail) { t = tail; } 
+4
source

First code:

 t != (t = tail) 

will assign tail t, then compare t with the new value

the second compares t with the tail

+3
source

Note: another answer is what Java really does

 synchronized(x) { if(t != (t = tail)); } 

equivalently

 synchronized(x) { t = tail; if(t != t) { // ... } } 

basically, a reference to what is assigned is returned by the operator ()

 public class Test { public static void main(String[] args) { Integer a = 1; Integer b = 2; if(a != (b = a)) { System.out.println("however, there is an issue with a != (a = b), Java bug"); } else { System.out.println("assignment first, obvious by inspection"); } } } 

However, the same code works in C. If I had to guess, it is unintentional in Java, any deviation from C for something like stupidity . Oracle probably does not expect obsolescence, suggesting that this is an unintended error, which probably is.

I will come to him the next time I talk to them. I'm still angry with them about the whole DOD fiasco of DOD related to setting my mom's homepage on ask.com in collusion with Apple Incorporated. Apple fixed iTunes less than a week after I had to click on this thing more than 400 times to retry trying to reload in my child video library, so the problem is limited to Oracle. This affected Microsoft, so everyone was delighted with it.

 #include <iostream> static int ref = 0; class t { public: t(int x) : x(x), r(ref) { ref++; } t(const t& o) : x(ox), r(or) { } t& operator=(const t& o) { x = ox; r = or; return *this; } bool operator!=(const t& o) const { return r != or; } private: int x; int r; }; int main() { ta(1); tb(2); if(a != (a = b)) { std::cout << "assignment\n"; } else { std::cout << "no assignment\n"; } return 0; } 
-3
source

Source: https://habr.com/ru/post/1274788/


All Articles