|
These logical operators work only on boolean operands. Their return values are always boolean.
?: Ternary if-then-else
& Logical AND
&& Short-circuit AND
| Logical OR
|| Short-circuit OR
^ Logical XOR
== Equal to
!= Not equal to
! Logical unary NOT
Now we will see the usage of these operators.
?: Ternary if-then-else
public class TernaryOperatorExample {
public static void main(String[] args) {
char ans = 'y';
// (?) condition is checked
// (:) value is returned based on the condition. If
// it is true then the first expression is returned
// otherwise the second one.
String value = ans == 'y' ? "IT'S YES" : "OH NO!!!";
System.out.println(value);
}
}
& Logical AND
public class ANDOperatorExample {
public static void main(String[] args) {
char ans = 'y';
int count = 1;
if(ans == 'y' & count == 0) {
System.out.println("Count is Zero.");
}
if(ans == 'y' & count == 1) {
System.out.println("Count is One.");
}
if(ans == 'y' & count == 2) {
System.out.println("Count is Two.");
}
}
}
&& Short-circuit AND
public class ShortCircuitANDOperatorExample {
public static void main(String[] args) {
int hour = 9;
int minute = 30;
// short circuit AND operator is differnt from
// normal AND operator. If happens like this that
// the when first condition is checked and if it
// is true then only it checks for the second condition
// that is on the other side of the &&.
if (hour == getHour() && minute == getMinute()) {
System.out.println("It is 10:30");
}
}
public static int getHour() {
System.out.println("Return Hour");
return 10;
}
public static int getMinute() {
System.out.println("Return Minute");
return 30;
}
}
Logical OR
public class ORExample {
public static void main(String[] args) {
int x = 10;
if(x == 5 | x ==10) {
System.out.println("Value of x can be divided by 10");
}
}
}
Short-circuit OR
public class ShortCircuitORExample {
public static void main(String[] args) {
int hour = 10;
int minute = 30;
// if the first condition is true then it will not
// check for the second condition. But if the first
// condition is false then it will check for second
// condition. In case of OR either one of the conditions
// should be true.
if(hour == getHour() || minute == getMinute()) {
System.out.println("Correct time of display this message");
}
}
public static int getHour() {
System.out.println("returning hour");
return 10;
}
public static int getMinute() {
System.out.println("returning minute");
return 30;
}
}
Logical XOR
public class XORExample {
public static void main(String[] args) {
int a = 1;
int b = 2;
int c = 3;
int d = 4;
// XOR will return true only if the conditions
// are mutually exclusive.
System.out.println(a == 1 ^ b == 2); // both the conditions are true
System.out.println(c == 4 ^ d == 3); // both the conditions are false
System.out.println(c == 4 ^ d == 4); // only second condition is true
}
}
Equal to
public class EqualToExample {
public static void main(String[] args) {
char ans = 'y';
if(ans == 'y') {
System.out.println("It is YES.");
}
}
}
Not equal to
public class NotEqualToExample {
public static void main(String[] args) {
int value = 4;
if (value != 10) {
System.out.println("Value is not equal to 10");
}
}
}
Logical unary NOT
public class UnaryNotExample {
public static void main(String[] args) {
boolean flag = false;
// Here the value of the flag is false. But if block
// is only executed when the condition is true. So
// in cases we can use Unary Not (!). This negates the
// boolean value of the flag. If the value of flag is
// false and used with Unary Not then it will return
// true and vice-versa.
if(!flag) {
System.out.println("Hello world.");
}
}
}
|