Page 1 of 1

About Question enthuware.ocajp.i.v7.2.977 :

Posted: Tue Dec 01, 2015 5:14 pm
by danielfb
In

Code: Select all

boolean b1 = false;
boolean b2  = false; 
if (b2 = b1 != b2)
Why is, according to your answer, "b1 != b2" evaluated before the "b2 = b1"?
Is this because of the != having a higher precedence?

Thanks,
Dani.

Re: About Question enthuware.ocajp.i.v7.2.977 :

Posted: Tue Dec 01, 2015 8:27 pm
by admin
Yes, assignment operator has the least precedence.

Re: About Question enthuware.ocajp.i.v7.2.977 :

Posted: Sat Feb 18, 2017 2:13 am
by Deleted User 3513
Just wanted to clarify again, in the line if(b2=b1!=b2), first it evaluates b1!=b2 which returns false and then it is assigned to b2. Here b2 is evaluated in the if-condition which has the value of false and that results to the execution of the else block?

Re: About Question enthuware.ocajp.i.v7.2.977 :

Posted: Sat Feb 18, 2017 2:44 am
by admin
That is correct.

Re: About Question enthuware.ocajp.i.v7.2.977 :

Posted: Wed Jan 31, 2024 3:17 am
by Bilbo2001
It turns out that the value is "true".

Two boolean variables b1 and b2 are declared and initialized with the value false.
Then, the condition b2 = b1 = !b2 is executed.
First, !b2 negates the value of b2, turning it into true since b2 is initially false.
This true value is then assigned to b1, making b1 also true.
Finally, this new true value is assigned to b2, making b2 true as well.
As a result, the condition becomes true because both variables b1 and b2 are now true.
"true" is printed to the console.

Re: About Question enthuware.ocajp.i.v7.2.977 :

Posted: Wed Jan 31, 2024 8:19 am
by admin
The expression given in the question does not have b1 = !b2. It has b1 != b2. Please read the code carefully.