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.
Let’s consider the following table for the result of each operation on a specific input.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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) |
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. |
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.
Note: in simple & operation, it checks both operands’ values, i.e. Operand1 & Operand 2. In short circuit AND operation && it checks the value of the first Operand1 later it will go with the value of operand 2 if and only if operand 1 value is true.Syntax:
Operand1 & Operand2
Operand1 and Operand2 are any Boolean values.
Output:
Truth Table of AND:
|
Bpackage 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...."); } } } Copy after login |
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) |
Output:
Operand1 || Operand2
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).
Output:
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) |
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.."); } } }
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) |
!Operand or ! Condition Copy after login |
TRUE (1) | TRUE (1) |
Output:
Operand holds any Boolean value. Condition is any Boolean value, i.e. Result of any Logical operation.
A | !A |
FALSE (0) | TRUE (1) |
TRUE (1) | FALSE (0) |
A | !A |
FALSE (0) | TRUE (1) |
TRUE (1) | FALSE (0) |
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.<em>out</em>.println("The Condition is True...."); } } }
Output:
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:
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) |
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:
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.
The above is the detailed content of Logical Operators in Java. For more information, please follow other related articles on the PHP Chinese website!