Java Reference
In-Depth Information
System.out.println ("b = " + b);
System.out.println ("i = " + i);
System.out.println ("j = " + j); // Will print j = 10
The above piece of code will print
b = false
i = 10
j = 10
Logical AND Operator (&)
The logical AND operator ( & ) is used in the form
operand1 & operand2
The logical AND operator returns true if both operands are true . If either operand is false , it returns false . The
logical AND operator ( & ) works the same way as the logical short-circuit AND operator ( && ), except for one difference.
The logical AND operator ( & ) evaluates its right-hand operand even if its left-hand operand evaluates to false .
int i = 10;
int j = 15;
boolean b;
b = (i > 5 & j > 10); // Assigns true to b
b = (i > 25 & ((j = 20) > 15)); // ((j = 20) > 5) is evaluated even if i > 25 returns false
System.out.println ("b = " + b);
System.out.println ("i = " + i);
System.out.println ("j = " + j); // Will print j = 20
The above piece of code will print
b = false
i = 10
j = 20
Logical Short-Circuit OR Operator (||)
The logical short-circuit OR operator ( || ) is used in the form
operand1 || operand2
The logical short-circuit OR operator returns true if either operand is true . If both operands are false , it returns
false . It is called a short-circuit OR operator because if operand1 evaluates to true , it returns true without evaluating
operand2 .
int i = 10;
int j = 15;
boolean b = (i > 5 || j > 10); // Assigns true to b
 
Search WWH ::




Custom Search