Logical Operators in Java

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 NameOperatorExample
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

XYOUTPUT
TRUETRUETRUE
TRUEFALSEFALSE
FALSETRUEFALSE
FALSEFALSEFALSE
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

XYOUTPUT
TRUETRUETRUE
TRUEFALSETRUE
FALSETRUETRUE
FALSEFALSEFALSE
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

XOUTPUT
TRUEFALSE
FALSETRUE
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



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *