Home  >  Article  >  Java  >  Logical Operators in Java

Logical Operators in Java

王林
王林Original
2024-08-30 15:19:18965browse

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.

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)

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.

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:

  1. True: the result is True if and only if both operand values are True.
  2. 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)
A
B
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....");
}
}
}

A & B

FALSE (0) FALSE (0) FALSE (0)

FALSE (0)Logical Operators in Java

TRUE (1) FALSE (0)
TRUE (1)

FALSE (0) FALSE (0)
TRUE (1)

TRUE (1) TRUE (1)
Example of AND Operator

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).

    Note:
  • Java uses two Logical OR operations, simple OR – “|” and Short circuit OR – “||”.in simple logical OR operation, both operand values are checked, and depending on the values it gives the result.in Short Circuit OR operation “||” it checks the value of the first operand, i.e. Operand1, and then checks the value of the second operand, i.e. operand2 either operand1 value is true or false. Syntax:
  • Operand1 and Operand2 are any Boolean values.

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)
True:
If both operand values are True. Suppose anyone Operand value is True.
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.

Logical Operators in Java

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
TRUE (1)

TRUE (1) TRUE (1)
Example of OR Operator

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 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)
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.<em>out</em>.println("The Condition is True....");
}
}
}

Output:

Logical Operators in Java

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:

Logical Operators in Java

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.

The above is the detailed content of Logical Operators in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Unary Operators in JavaNext article:Unary Operators in Java