Monday 23 May 2011

Logical or Conditional Operators in java

Logical operators return a true or false value based on the state of the Variables. There are six logical, or boolean, operators. They are AND, conditional AND, OR, conditional OR, exclusive OR, and NOT. Each argument to a logical operator must be a boolean data type, and the result is always a boolean data type.
Symbol Name
AND &&
OR ||
NOT !
An example program is shown below that demonstrates the different Logical operators in java.
Example of &&
boolean b;
b = 3 > 2 && 5 < 6; // b is true
b = 2 > 3 && 5 < 6; // b is now false


Example of ||


boolean b;
b = 3 > 2 || 5 < 6; // b is true
b = 2 > 3 || 5 < 6; // b is still true

Example of !

boolean b;
b = !(3 > 2); // b is false
b = !(2 > 3); // b is true

Also there are bitwise logical operators also, please have a look.
Also see important rule for conditional operators - || and &&

No comments:

Post a Comment