Logical operators in Java Programming
Previous | Home | Next |
Java provides an easy way to handle multiple conditions: the logic operators.
There are three logic operators,&&
,||
and!
.
&& is logical and.
&& combines two boolean values and returns a boolean which is true if and only if both of its operands are true. .
|| is logical OR
|| combines two boolean variables or expressions and returns a result that is true if either or both of its operands are true.
The last logic operator is ! which means not.
It reverses the value of a boolean expression. Thus if b is true !b is false. If b is false !b is true.
b = 3 > 2 && 5 < 7; // b is true
b = 8 > 3 || 5 < 7; // b is true
boolean b;
b = !(5 > 2); // b is false
b = !(2 > 5); // b is true
public class LogicalOperators
{
public LogicalOperators()
{
Boolean x = true;
Boolean y = false;
System.out.println(" x & y: " +(x & y));
System.out.println(" x | y :" + (x | y));
System .out.println(" x && y :"+ (x && y));
public static void main (String arg[])
{
new Logicaloperators();
}
}
Previous | Home | Next |