Logical operators are used to make logical decisions. Logical operators are mainly used in conditional statements and loops to make logical decisions.



Logical Operators in Java

There are three kinds of logical operators.

Operator Name Operator Example
Logical AND && x && y
Logical OR || x || y
Logical NOT ! !x


Logical AND Operator

Logical AND operator is used to perform AND operators as shown in the below table

X Y OUTPUT
TRUE TRUE TRUE
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE FALSE
package com.yawintutor;

public class LogicalAND {
	public static void main(String[] args) {
		boolean a = true;
		boolean b = false;
		boolean c;

		c = a && b;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		System.out.println("\n(a && b) is " + c);

	}
}

Output

a value is true
b value is false

(a && b) is false


Logical OR Operator

Logical OR operator is used to perform OR operators as shown in the below table

X Y OUTPUT
TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE
package com.yawintutor;

public class LogicalOR {
	public static void main(String[] args) {
		boolean a = true;
		boolean b = false;
		boolean c;

		c = a || b;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		System.out.println("\n(a || b) is " + c);

	}
}

Output

a value is true
b value is false

(a || b) is true


Logical NOT operator

Logical NOT operator is used to perform NOT operators as shown in the below table

X OUTPUT
TRUE FALSE
FALSE TRUE
package com.yawintutor;

public class LogicalNOT {
	public static void main(String[] args) {
		boolean a = true;
		boolean b;

		b = !a;
		

		System.out.println("a value is " + a);
		System.out.println("\n( !a )  is " + b);

	}
}

Output

a value is true

( !a )  is false



Leave a Reply