This operator is used to perform logical operations like logical AND, logical OR, logical NOT etc.,
There are different ways to use logical operators. Let us discuss below
&&: This operator will verify and return true if left and right operands are true otherwise it will return false
The below examples verifies both operands ‘x’ and ‘y’ and returns False. Also it verifies ‘a’ and ‘b’ and returns True
package com.test.basics;
public class TestLogicalOperator {
public static void main(String[] args) {
boolean x = true;
boolean y = false;
boolean negativeResult = (x && y);
System.out.println("Validating the Logical AND -VE result " + negativeResult);
boolean a = true;
boolean b = true;
boolean positiveResult = (a && b);
System.out.println("Validating the Logical AND +VE result " + positiveResult);
}
}

||: This operator will verify and return true if either left and right operands are true it will only returns false if both operands are false
The below examples verifies both operands ‘x’ and ‘y’ and returns True. Also it verifies ‘a’ and ‘b’ and returns False
package com.test.basics;
public class TestLogicalOROperator {
public static void main(String[] args) {
boolean x = true;
boolean y = false;
boolean positiveResult = (x || y);
System.out.println("Validating the Logical OR +VE result " + positiveResult);
boolean a = false;
boolean b = false;
boolean negativeResult = (a || b);
System.out.println("Validating the Logical AND -VE result " + negativeResult);
}
}

!: This operator will verify and return true if both left and right operands are false and it will returns true if both operands are false
The below examples verifies both operands ‘x’ and ‘y’ and returns False because both x and y are true where as !(true) is false . Also it verifies ‘a’ and ‘b’ and returns True because both a and b are false where as !(false) is true
package com.test.basics;
public class TESTLogicalNOT {
public static void main(String[] args) {
boolean x = true;
boolean y = true;
boolean negativeResult = !(x && y);
System.out.println("Validating the Logical AND -VE result " + negativeResult);
boolean a = false;
boolean b = false;
boolean positiveResult = !(a && b);
System.out.println("Validating the Logical AND +VE result " + positiveResult);
}
}
