Logical operators in java are the building blocks used to perform functions on variables and values. In this post, you can find logical operators example in Java. We can use many different operators according to our needs for calculations and functions. Operators in java can be logical, ternary, bitwise arithmetic, relational operators etc. Though they are called logical operators, users can apply them on any type. It is not necessary to perform the operation on Boolean values only.
What are Logical operators in Java?
Java Logical Operators perform operations such as AND, OR, NOT. These functions are similar to AND gates or OR gates in electronics. They help in combining two conditions to make one final output. We always keep in mind the short-circuiting effect which says that the second value is never evaluated if the first condition is false. Use of logical operators mainly for decision making. We can also use it in places where we have to get results as explicit true or explicit false.
Types of Logical operators
1.) Logical AND operator: This operator tends to return a true value if the satisfactory conditions are true. If one of the 2 values is false, it returns false. However, returns true if both the conditions are true. It is denoted by &&.
AND( true || true); = true
AND( false || true); = false
AND( true || false); = false
AND( false || false); = false
2.) Logical OR operator in Java: We use vertical lines to represent the operator. It returns true if one of the two conditions is true. If even one condition is true, the output will be true. For false, both conditions need to be false. It is denoted by ||
OR( true || true); = true
OR( false || true); = true
OR( true || false); = true
OR( false || false); = false
3.) Logical NOT operator: The operator returns the exact opposite value of the condition. For example, if the condition is true, the output of the “NOT” operator will be false. And if the condition will be false, the output of the “NOT” operator will be true. It will simply negate a statement. The statement below means when the condition is not (!) true, t is false. And when the condition is not(!) false, it is true.
NOT( !true); = false
NOT( !false); = true
Logical Operators Example in Java
public class Test {
public static void main(String args[]) {
boolean x = true;
boolean y = false;
System.out.println("x && y = " + (x&&y));
System.out.println("x || y = " + (x||y) );
System.out.println("!(x && y) = " + !(x && y));
}
}
The output of the above java program will be:
x && y = false
x || y = true
!(x && y) = true
Logical Operators Program in Java
public class DeveloperHelps {
public static void main(String[] args) {
int a = 25;
int b = 40;
System.out.println("a & b= " + (a & b));
System.out.println("a | b= " + (a | b));
System.out.println("a ^ b= " + (a ^ b));
System.out.println("~a= " + ~a);
boolean a1 = true;
boolean a2 = true;
boolean a3 = false;
System.out.println("val1 & val2= " + (a1 & a2));
System.out.println("val2 & val3= " + (a2 | a3));
System.out.println("val2 | val3= " + (a2 | a3));
System.out.println("val1 ^ val2= " + (a1 ^ a2));
System.out.println("!val1= " + !a1);
}
}
The output of the above java program will be:
a & b= 8
a | b= 57
a ^ b= 49
~a= -26
val1 & val2= true
val2 & val3= true
val2 | val3= true
val1 ^ val2= false
!val1= false