This article lists and explains boolean operators available in java.
Boolean operators
Operators
Meaning
a && b
Conditional AND.
If both variables a and b are true, the result is true.
If either of the variable is false, the result is false.
If a is false then b is not evaluated.
a & b
Boolean AND.
If both variables are true, the result is true.
If either of the variable is false, the result is false.
a || b
Conditional OR.
If either of the variable is true, the result is true.
If a is true, b is not evaluated.
a | b
Boolean OR.
If either of the variable is true, the result is true.
! a
Boolean NOT.
If a is true, the result is false.
If a is false, the result is true.
a ^ b
Boolean XOR.
If a is true and b is false, the result is true.
If a is false and b is true, the result is true.
In other cade, the result is false.
Examples of Boolean Operators
Example 1 : Program that displays use of boolean operators i.e &&, &, ||, |, !, ^
class BoolOptrDemo
{
public static void main(String args[])
{
boolean b1 = true;
boolean b2 = false;
System.out.println("b1|b2 = "+(b1|b2));
System.out.println("b1&b2 = "+(b1&b2));
System.out.println("!b1 = "+(!b1));
System.out.println("b1^b2 = "+(b1^b2));
System.out.println("(b1|b2)&b1 = "+((b1|b2)&b1));
}
}
Operators
|
Meaning
|
a && b
|
Conditional AND.
If both variables a and b are true, the result is true.
If either of the variable is false, the result is false.
If a is false then b is not evaluated.
|
a & b
|
Boolean AND.
If both variables are true, the result is true.
If either of the variable is false, the result is false.
|
a || b
|
Conditional OR.
If either of the variable is true, the result is true.
If a is true, b is not evaluated.
|
a | b
|
Boolean OR.
If either of the variable is true, the result is true.
|
! a
|
Boolean NOT.
If a is true, the result is false.
If a is false, the result is true.
|
a ^ b
|
Boolean XOR.
If a is true and b is false, the result is true.
If a is false and b is true, the result is true.
In other cade, the result is false.
|
{
public static void main(String args[])
{
boolean b1 = true;
boolean b2 = false;
System.out.println("b1|b2 = "+(b1|b2));
System.out.println("b1&b2 = "+(b1&b2));
System.out.println("!b1 = "+(!b1));
System.out.println("b1^b2 = "+(b1^b2));
System.out.println("(b1|b2)&b1 = "+((b1|b2)&b1));
}
}
No comments:
Post a Comment