In this tutorial, we will Explore Various Logical Operators Supported in Java such as NOT, OR, XOR Java or Bitwise Exclusive Operator in Java With Examples:

In one of our earlier tutorials on Java Operator, we saw the different types of operators available in Java. Here, we will explore the Logical Operators supported by Java in detail.

**First, let’s see what Logical Operators are?**

**=> Check Out The Complete FREE Java Course Here**

Table of Contents:

### What Are Logical Operators?

**Java supports the following conditional operators that are also called as Logical Operators:**

Operator | Description |
---|---|

&& | Conditional-AND |

|| | Conditional-OR |

! | Logical NOT |

**Java also supports the following** **Bitwise Logical Operator**:

^ | Bitwise exclusive OR Also known as XOR |

These logical operations are performed on two Boolean expressions.

**Let’s see these operators in details :**

**&&**: This operator is called as**Conditional-AND**. Here, && performs conditional AND on two boolean expressions.

**For Example,**

public class LogicalOperatorsDemo1 { public static void main(String[] args) { boolean x = true;//boolean variable x is intialiized with value true boolean y = false;//boolean variable y is intialiized with value false boolean z = (x && y) ;//conditional-AND on x and y System.out.println("x && y = " + z);//print value of the result //This gives an output x && y = false } }

**This program prints the following output:**

Here, x and y are two boolean values.

**&&** performs **Conditional-AND** on x=true and y=false , it returns true&&false i.e. false

**||:**This operator is called as**Conditional-OR**. Here, || performs conditional OR on two boolean expressions.

**For Example,**

public class LogicalOperatorsDemo2 { public static void main(String[]args) { boolean x = true;//boolean variable x is intialiized with value true boolean y = false;//boolean variable y is intialiized with value false boolean z = (x || y) ; //conditional-OR on x and y System.out.println("x || y = " + z);//print value of the result //This gives an output x || y = true } }

**This program prints the following output:**

Here, x and y are two **boolean values**.

**|| **performs **Conditional-OR** on x=true and y=false , it returns true||false i.e. true

**!**: This is called a logical complement operator. This is performed on a single operand. This operator inverts the value of a boolean.

**For Example,**

public class LogicalOperatorsDemo3 { public static void main(String[]args) { boolean x = true;//boolean variable x is intialiized with value true boolean z = !x; // inverting the value of x System.out.println("z = " + z);//print value of the result //This gives output as z = false } }

**This program prints the following output:**

In the above program, ! returns an inverted value of the boolean variable value x i.e.!(true) i.e. false.

### Bitwise Exclusive OR – XOR Java

**Now let’s see Java Bitwise Operator i.e. XOR Operator in detail:**

^ | Bitwise exclusive OR Also known as XOR |

Bitwise exclusive OR or XOR ^ is binary operator performs a bit by bit exclusive OR operation.

**It performs the operation as follows:**

- If both the bits are the same, then the XOR operator returns the result as
**‘0’.** - If both the bits are different, then the XOR operator returns the result as
**‘1’.**

x | y | x ^ y |
---|---|---|

true | false | true |

true | true | false |

false | true | true |

false | false | false |

**XOR operator follows an evaluation order from the left to right order.**

**Let us have a look at the following Java sample that illustrated the use of Java xor Operators:**

public class XorDemo { public static void main(String[] args) { boolean a = true; boolean b = false; boolean result = a ^ b; System.out.println("a ^ b: "+ result); //prints the result true a = true; b = true; result = a ^ b; System.out.println("a ^ b: "+ result); //prints the result false a = false; b = true; result = a ^ b; System.out.println("a ^ b: "+ result); //prints the result true a = false; b = false; result = a ^ b; System.out.println("a ^ b: "+ result); //prints the result false } }

**This program prints the following output:**

**Let’s see how this XOR operation takes place for integer values with the following example:**

To perform Java XOR operation on integer values like int 6 and int 10,

XOR happens on binary values of 6 i.e. 0110 and 10 i.e. 1010.

**So XOR on 6 and 10 as follows :**

**0110**

** ^**

** 1010**

**=======**

** 1100**

Result returned is the integer value of 1100 is 12

**Given below is the sample Java program to perform XOR on two integers:**

public class XorDemo1 { public static void main(String[] args) { int x = 6;// Binary value of 6 is 0110 int y = 10;// Binary value of 10 is 1010 int result = x^y;// xor operation on 0110^1010 which gives 1100 System.out.println("result: "+result);//integer value of 1100 is 12 } }

**This program prints the following output:**

### Frequently Asked Questions And Answers

**Q #1) What is the XOR operation?**

**Answer:** Bitwise exclusive OR or XOR ^ is a binary operator that performs a bit by bit exclusive OR operation.

**Q #2) How is XOR calculated?**

**Answer:** Bitwise exclusive OR or XOR ^ performs a bit by bit exclusive OR operation as follows:

- If both the bits are the same, then the XOR operator returns the result as
**‘0’.** - If both the bits are different, then the XOR operator returns the result as
**‘1’.**

**Q #3) What is the difference between && and & in Java?**

**Answer: &&:** This is Conditional-AND performed on two boolean operands.

Whereas, **&** is a bitwise AND operator which is performed on bit operands.

**Q #4) What is OR operator in Java?**

**Answer: **Java supports **Conditional-OR** i.e.** ||** Here, ||performs conditional OR on two boolean expressions.

**For Example,**

boolean x = true;

boolean y = false;

(x || y ) returns true

**Q #5) What is the symbol of OR in Java?**

**Answer: **Java supports Conditional-OR having symbol ||. This is different than the exclusive or bitwise operation and it has symbol ^.

**Q #6) What is the use of Bitwise Operators in Java?**

**Answer: **Bitwise operators in Java are used for manipulating bits of a number. They can be used with data types like char, short, int, etc.

### Conclusion

In this tutorial, we have explored the following logical operators supported in Java in detail with the help of sample programs.

**&& : Conditional-AND****|| : Conditional-OR****! : Logical NOT**

**We also discussed the following operator:**

**^ : Bitwise exclusive or XOR**

**=> Read Through The Java Beginners Training Series Here**