question archive Can a boolean variable equate itself? For example, if booleanVariable has been initialized as True, and we have a booleanMethod() checking whether something evaluates to true/false; would the second underlined statement be doing a comparison between the booleanVariable and the booleanMethod result and saving said result in booleanVariable? If so, why not just equate booleanVariable to the booleanMethod() itself? Example code: boolean booleanVariable = True; //initializing booleanVariable booleanVariable = booleanVariable && booleanMethod();
Subject:Computer SciencePrice:4.86 Bought7
Can a boolean variable equate itself?
For example, if booleanVariable has been initialized as True, and we have a booleanMethod() checking whether something evaluates to true/false; would the second underlined statement be doing a comparison between the booleanVariable and the booleanMethod result and saving said result in booleanVariable? If so, why not just equate booleanVariable to the booleanMethod() itself?
Example code:
boolean booleanVariable = True; //initializing booleanVariable
booleanVariable = booleanVariable && booleanMethod();
The answer lies in the logic rules behind the "&&" or AND operator.
Step-by-step explanation
Remembering '&&' logic Rules:
'(NOTE: the logic operator can also be represented as * symbol. T = True and F = false)' n * T = n n * F = F
Assuming that booleanVariable is ALWAYS true
Following the rules above, assuming that booleanVariable is ALWAYS True, then the boolean result will only be varied based on the other operand. In this scenario, booleanVariable is your first operand, which is always T. Then n will be your other operand which will be booleanMethod().
Which basically means you are correct, we don't have to include booleanVariable in the comparison, since it is always true and will not affect our results.
'Summary, since booleanVariable is always true, the result of our boolean expression is COMPLETELY based on the 2nd operand, which is booleanMethod().' booleanVariable = booleanMethod(); 'Examples:' boolean booleanVariable = true; //always set to true 'if booleanMethod is false, the entire boolean expression(right side of = sign) is false. Otherwise, returns true.' booleanVariable = booleanVariable && booleanMethod(); 'Produces the same result as above. If booleanMethod is false, returns false. Otherwise, returns true.' booleanVariable = booleanMethod();
Assuming booleanVariable can be initialized or set to false pre-comparison
In this scenario, both operands matter (booleanVariable and booleanMethod). Since we cannot skip our check on whether booleanVariable is false. Hence, we need to compare with both operands and simplifying by directly assigning the booleanMethod() is not possible.
booleanVariable = booleanVariable && booleanMethod(); 'Example errors if we remove booleanVariable (assume that booleanMethod() is true):' boolean booleanVariable = false; booleanVariable = booleanMethod(); //sets booleanVariable to True, even though it should be false when using the && operator booleanVariable = booleanVariable && booleanMethod(); //sets booleanVariable to false wwhich is the correct answer