Micael Illos Micael Illos - 1 year ago 66
Java Question

How Do Boolean values work When they are assigned to themselves?

I have a part of code and im trying to understand what a certain line does.

boolean computer = true;
boolean rs = false;
computer = (computer || rs) && !(computer && rs);
`


The line that I want to understand is:

computer = (computer || rs) && !(computer && rs);


If
computer
is
true
and
rs
is
false
, what happens?

And if its the other way around?

Or even if they are both equal to
true
?

Answer Source

You need to evaluate the expressions in the brackets first and the apply operations precedence to find out the solution

boolean computer = true;
boolean rs = false;   
computer = (computer || rs) && !(computer && rs); 

( true || false ) && ! (true && false )
= true && !(false)
= true && true 
= true

Use this truth table to work out different scenarios

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download