Introduction to Logical Operators in Java
Logical operators are used for performing the operations on one or two variables for evaluating and retrieving the logical outcome. Typically, the return value for logical operations is in boolean format and is applied in a program to establish better control in the program’s execution flow. In Java, the logical operators used are ‘&’ for performing AND operation, ‘|’ for OR operation, ‘!’ for NOT operation, and ‘^’ for XOR operation.
Features of Logical Operators in Java
- Logical operators are used to controlling the flow of execution.
- Boolean logical operators always return a Boolean value.
- These operators are applied to one or more Boolean operands.
- Java provides 4 logical operators “&”,”|”,”!”or”~” and “^”.
Let’s consider the following table for the result of each operation on a specific input.
A | B | A&B | A|B | A^B | !A or ~A |
True (1) | True(1) | True (1) | True(1) | False (0) | False (0) |
True(1) | False(0) | False(0) | True(1) | True(1) | False(0) |
False(0) | True(1) | False(0) | True(1) | True(1) | True(1) |
False(0) | False(0) | False(0) | False(0) | False(0) | True(1) |
Different Logical Operators in Java with Description
The following table shows the operator and its description.
Operator | Meaning | Description |
& | Logical AND | If both the inputs are True, then the result is True; if anyone input is False, the result will be False. |
| | Logical OR | The result is True if any of the input is True. The result will be false if both the inputs are False. |
! or ~ | Logical NOT | Logical NOT is a unary operator; it operates only on a single operand. It always outputs the negation of input value. |
^ | Logical XOR | The result is True if any one of the input is True. The result will be false if both the inputs are the Same. |
1. Logical AND Operator “&”
The logical “&” operator performs the digital AND operation. This operator works on two Boolean operands, and the result will be Boolean. AND operator represented by the symbol “&” or “&&” i.e. short circuit AND operation.
Syntax:
Operand1 & Operand2
Operand1 and Operand2 are any Boolean values.
Output:
- True: the result is True if and only if both operand values are True.
- False: the result is False when any one of the operand values is false. And if both values are False.
Truth Table of AND:
A | B | A & B |
FALSE (0) | FALSE (0) | FALSE (0) |
FALSE (0) | TRUE (1) | FALSE (0) |
TRUE (1) | FALSE (0) | FALSE (0) |
TRUE (1) | TRUE (1) | TRUE (1) |
Example of AND Operator
package com.java.demo;
public class DemoAND
{
public static void main(String[] args)
{
boolean a=true;
boolean b=false;
int num1=0;
int num2=1;
boolean out1=(a & a);
boolean out2=(a & b);
boolean out3=(b & a);
boolean out4=(b & b);
System.out.println("True & True :"+out1);
System.out.println("True & False :"+out2);
System.out.println("False & True :"+out3);
System.out.println("False & False :"+out4);
if(num1 ==0 & num2 == 1)
{
System.out.println("The Condition is True....");
}
}
}
Output:
2. Logical OR Operator “ |.”
Logical OR operator in java is used to perform actual digital OR operations in java. This operator is used with two Boolean operands, and the result will be Boolean, i.e. true or False. In java, the Logical OR operator is represented with the symbol “|” (Simple OR) or “||” (Short Circuit OR).
Syntax:
Operand1 || Operand2
Operand1 and Operand2 are any Boolean values.
Output:
- True: If both operand values are True. Suppose anyone Operand value is True.
- False: If both operand values are False.
Truth Table of OR:
A | B | A |B |
FALSE (0) | FALSE (0) | FALSE (0) |
FALSE (0) | TRUE (1) | TRUE (1) |
TRUE (1) | FALSE (0) | TRUE (1) |
TRUE (1) | TRUE (1) | TRUE (1) |
Example of OR Operator
package com.java.demo;
public class DemoOR
{
public static void main(String[] args)
{
boolean a=true;
boolean b=false;
int num=0;
boolean out1=(a | a);
boolean out2=(a | b);
boolean out3=(b | a);
boolean out4=(b | b);
System.out.println("True | True :"+out1);
System.out.println("True | False :"+out2);
System.out.println("False | True :"+out3);
System.out.println("False | False :"+out4);
if(num == 0 | num == 1)
{
System.out.println("The Number is binary..");
}
}
}
Output:
3. Logical NOT Operator “!” or “ ~.”
Logical NOT operator performs actual digital NOT operation in java, i.e. Negation of Input value. This logical operator is a Unary Logical operator; it is used with only one operand.in java Logical NOT operator is represented by the symbol “!” or “ ~ ”.simple use of! The operator is to negating the input value. For example, input, True make it False or if the input is False to make it True.
Syntax:
!Operand or ! Condition
Operand holds any Boolean value. Condition is any Boolean value, i.e. Result of any Logical operation.
Result:
- True: The result is True if the input value is False.
- False: The result is False if input values are True.
Truth Table of NOT:
A | !A |
FALSE (0) | TRUE (1) |
TRUE (1) | FALSE (0) |
Example of NOT Operator
public class DemoNOT
{
public static void main(String[] args)
{
boolean a=true;
boolean b=false;
int num1=0;
int num2=1;
boolean out1=(a ^ a);
boolean out2=(a ^ b);
boolean out3=(b ^ a);
boolean out4=(!b ^ b);
System.out.println("True ^ True :"+out1);
System.out.println("True ^ False :"+out2);
System.out.println("False ^ True :"+out3);
System.out.println(!b+" ^ False :"+out4);
System.out.println("a=true !a="+!a);
if(!(num1 ==0) ^ (num2 == 1))
{
System.out.println("The Condition is True....");
}
}
}
Output:
4. Logical XOR Operator “ ^.”
Logical XOR operator is a short form of Exclusive OR operator. This logical operator is when we have to check or compare the values of anyone operand is True then the output is true. In Java, Logical XOR is represented by the symbol “ ^ ”.This operator is Binary Logical Operator, i.e. can be used with two operands/conditions. Output this operator is also a Boolean value.
Syntax:
Operand1 ^ Operand2 or Condition1 ^ Condition2
Operand1 and Operand2 hold any Boolean values. Condition1 and Condition2 hold any Boolean values, i.e. output any logical operation.
Output:
- True: The result is True if any one of the input is True.
- False: The result is False if both the inputs are the Same.
Truth Table of XOR:
A | B | A ^ B |
FALSE (0) | FALSE (0) | FALSE (0) |
FALSE (0) | TRUE (1) | TRUE (1) |
TRUE (1) | FALSE (0) | TRUE (1) |
TRUE (1) | TRUE (1) | FALSE (0) |
Example of XOR Operator
public class DemoXOR
{
public static void main(String[] args)
{
boolean a=true;
boolean b=false;
int num1=0;
int num2=1;
int num3=5;
int num4=7;
boolean out1=(a ^ a);
boolean out2=(a ^ b);
boolean out3=(b ^ a);
boolean out4=(b ^ b);
System.out.println("True ^ True :"+out1);
System.out.println("True ^ False :"+out2);
System.out.println("False ^ True :"+out3);
System.out.println("False ^ False :"+out4);
System.out.println("5 ^ 7 ="+(num3 ^ num4));
System.out.println("0101 ^ 0111=0010");
if((num1 ==2) ^ (num2 == 1))
{
System.out.println("The Condition is True....");
}
}
}
Output:
Conclusion
It makes java code more powerful and flexible. Use logical operators in conditional statements or looping statements to look very clean. The most important benefit of the logical operator is it reduces the code complexity. For example, it reduces the number of if…else conditional statements. This indirectly benefits in code compilation, run time etc.…overall code performance is increased.
Recommended Articles
This is a guide to Logical Operators in Java. Here we discuss the introduction and different logical operators in java, i.e. AND, OR, NOT, XOR with Examples. You may also look at the following articles to learn more –