Logical Operators are those that are used for doing logical operations. There are a total of 6 logical operators (&, |, ^, !, &&, and ‖)
Bitwise Operators
Of the six logical operators listed above, three of them (&, |, and ^) can also be used as “bitwise” operators. Here are several legal statements that use bitwise operators:
byte b1 = 6 & 8;
byte b2 = 7 | 9;
byte b3 = 5 ^ 4;
System.out.println(b1 + " " + b2 + " " + b3);
Bitwise operators compare two variables bit by bit, and return a variable whose bits have been set based on whether the two variables being compared had respective bits that were either both “on” (&), one or the other “on” (|), or exactly one “on” (^). By the way, when we run the preceding code, we get
0 15 1
Note: These bit-wise operators are not in the SCJP 5 exam.
Short-Circuit Logical Operators
There are five logical operators on the exam that are used to evaluate statements that contain more than one boolean expression. The most commonly used of the five are the two short-circuit logical operators. They are
• && short-circuit AND
• || short-circuit OR
The && and || operators evaluate only boolean values. For an AND (&&) expression to be true, both operands must be true—for example
if ((2 < 3) && (3 < 4)) { }
The preceding expression evaluates to true because both operand one (2 < 3) and operand two (3 < 4) evaluate to true.
The short-circuit feature of the && operator is so named because it doesn’t waste its time on pointless condition evaluations. A short-circuit && evaluates the left side of the operation first (operand one), and if it resolves to false, the && operator doesn’t bother looking at the right side of the expression (operand two) since the && operator already knows that the complete expression can’t possibly be true.
class LogicalTest {
public static void main(String [] args) {
boolean b = true && false;
System.out.println("boolean b = " + b);
}
}
When we run the preceding code, we get
%java LogicalTest
boolean b = false
The || operator is similar to the && operator, except that it evaluates to true if EITHER of the operands is true. If the first operand in an OR operation is true, the result will be true, so the short-circuit || doesn’t waste time looking at the right side of the equation. If the first operand is false, however, the short-circuit || has to evaluate the second operand to see if the result of the OR operation will be true or false.
1. class TestOROperator {
2. public static void main(String[] args) {
3. if ((checkIfSmall(3)) || (checkIfSmall(7))) {
4. System.out.println("Result is true");
5. }
6. if ((checkIfSmall(6)) || (checkIfSmall(9))) {
7. System.out.println("Result is true");
8. }
9. }
10.
11. public static boolean checkIfSmall(int i) {
12. if (i < 5) {
13. System.out.println("i < 5");
14. return true;
15. } else {
16. System.out.println("i >= 5");
17. return false;
18. }
19. }
20. }
What is the result?
% java TestOROperator
i < 5
Result is true
i >= 5
i >= 5
Here’s what happened when the main() method was executed:
1. When we hit line 3, the first operand in the || expression (in other words, the left side of the || operation) is evaluated.
2. The checkIfSmall(3) method is invoked, prints “i < 5”, and returns true.
3. Because the first operand in the || expression on line 3 is true, the || operator doesn’t bother evaluating the second operand. So we never see the “i >= 5” that would have printed had the second operand been evaluated (which would have invoked checkIfSmall(7)).
4. Line 6 is evaluated, beginning with the first operand in the || expression.
5. The checkIfSmall(6) method is called, prints “i >= 5,” and returns false.
6. Because the first operand in the || expression on line 6 is false, the || operator can’t skip the second operand; there’s still a chance the expression can be true, if the second operand evaluates to true.
7. The checkIfSmall(9) method is invoked and prints “i >= 5”.
8. The checkIfSmall(9) method returns false, so the expression on line 6 is false, and thus line 7 never executes.
Logical Operators (Not Short-Circuit)
There are two non-short-circuit logical operators.
• & non-short-circuit AND
• | non-short-circuit OR
These operators are used in logical expressions just like the && and || operators are used, but because they aren’t the short-circuit operators, they evaluate both sides of the expression, always! For example, even if the first operand (left side) in an & expression is false, the second operand will still be evaluated—even though it’s now impossible for the result to be true! And the | is just as inefficient: if the first operand is true, the Java Virtual Machine (JVM) still plows ahead and evaluates the second operand even when it knows the expression will be true.
You’ll find questions on the exam that use both the short-circuit and non-short-circuit logical operators. You’ll have to know exactly which operands are evaluated and which are not, since the result will vary depending on whether the second operand in the expression is evaluated:
int z = 5;
if(++z > 5 || ++z > 6) z++; // z = 7 after this code
versus:
int z = 5;
if(++z > 5 | ++z > 6) z++; // z = 8 after this code
Logical Operators ^ and !
The last two logical operators on the exam are
• ^ exclusive-OR (XOR)
• ! boolean invert
The ^ (exclusive-OR) operator evaluates only boolean values. The ^ operator is related to the non-short-circuit operators we just reviewed, in that it always evaluates both the left and right operands in an expression. For an exclusive-OR (^) expression to be true, EXACTLY one operand must be true—for example
System.out.println("xor " + ((2<3) ^ (4>3)));
produces the output: xor false
The preceding expression evaluates to false because BOTH operand one (2 < 3) and operand two (4 > 3) evaluate to true.
The ! (boolean invert) operator returns the opposite of a boolean’s current value:
if(!(7 == 5)) { System.out.println("not equal"); }
can be read “if it’s not true that 7 == 5,” and the statement produces this output:
not equal
Here’s another example using booleans:
boolean t = true;
boolean f = false;
System.out.println("! " + (t & !f) + " " + f);
produces the output:
! true false
In the preceding example, notice that the & test succeeded (printing true), and that the value of the boolean variable f did not change, so it printed false.
Previous Chapter: Chapter 25: Arithmetic Operators
Next Chapter: Quick Recap - Chapters 22 to 26
Topics Covered in the Blog - Synopsis
Friday, February 4, 2011
Chapter 26: Logical Operators
Labels:
and,
logical and,
logical operators,
logical or,
or,
short circuit operators
| Reactions: |
Subscribe to:
Post Comments (Atom)
© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

No comments:
Post a Comment