Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In Java, logical operators evaluate boolean expressions and make decisions based on multiple conditions. The three main logical operators are AND (&&), OR (||), and NOT (!). These operators allow you to combine or negate boolean values, which enable you to create complex conditional statements. In this article, we will learn about the logical operators in Java.
In Java, logical operators "OR" (||), "AND" (&&), and "NOT" (!) execute operations analogous to digital electronics' AND and OR gates. They are utilized to combine two or more conditions or to invert the evaluation of a condition.
AND (&&): Both conditions must be true for the overall expression to be true. If the first condition is false, the second isn't evaluated—this is known as short-circuiting.
OR (||): If at least one condition is true, the overall expression is true. Here, short-circuiting occurs if the first condition is true, as the second condition isn’t checked.
NOT (!): This operator inverts the truth value of its operand; if the condition is true, NOT makes it false, and vice versa.
Logical OR operators, denoted by ‘||,’ return true if at least one of the conditions is true. It returns true if at least one of the conditions is true.
Syntax:
var = condition1 || condition2
You can also try this code with Online Java Compiler
class A {
public static void main (String[] args) {
boolean cond1 = true;
boolean cond2 = false;
if (cond1 || cond2){
System.out.println("At least one of the conditions is true");
}
else{
System.out.println("Both of the conditions are false");
}
}
}
You can also try this code with Online Java Compiler
The variable cond1 is true and variable cond2 is false. As at least one of the two variables is true, the logical OR of them is true, and hence if block is executed and the corresponding statement is printed.
class A {
public static void main (String[] args) {
boolean cond1 = true;
boolean cond2 = false;
if (cond1 && cond2){
System.out.println("Both of the conditions are true");
}
else{
System.out.println("At least one of the conditions is false");
}
}
}
You can also try this code with Online Java Compiler
The variable cond1 is true and variable cond2 is false. As at least one of the two variables is false, the logical AND of them is false, and hence else block is executed and the corresponding statement is printed.
Logical NOT operators are denoted by ‘!’. It's a unary operation that returns true if the condition under consideration is either not met or false. In other words, the operator returns true if the condition is false and false if the condition is true.
Syntax:
var = !condition
You can also try this code with Online Java Compiler
class A {
public static void main (String[] args) {
int var1 = 100;
int var2 = 101;
if (!(var1>var2)){
System.out.println("var2 is greater than var1");
}
else{
System.out.println("var1 is greater than var2");
}
}
}
You can also try this code with Online Java Compiler
The value of var1 is less than var2. Hence var1>var2 is false, and the logical NOT of it is true. Hence the condition of the if statement is satisfied and the corresponding block of the if statement is executed.
The logical XOR (eXclusive OR) operator is a binary operator that returns `True` if exactly one of its operands is `True`, and `False` otherwise. In other words, it returns `True` if the operands have different values, and `False` if they have the same value.
Let's see how it works: - If both operands are `True`, the XOR operator returns `False`. - If both operands are `False`, the XOR operator returns `False`. - If one operand is `True` and the other is `False`, the XOR operator returns `True`.
Syntax:
operand1 ^ operand2
Example:
Java
Java
public class XORExample { public static void main(String[] args) { boolean condition1 = true; boolean condition2 = false;
Boolean operators are logical operators that perform operations on boolean values (true or false) and return a boolean result.
What are the three main logical operators?
The three main logical operators are AND (&&), OR (||), and NOT (!). They perform logical operations on boolean values.
Is null a logical operator?
No, null is not a logical operator. It is a special value that represents the absence of an object reference.
Conclusion
In this article, we have extensively discussed logical operators and their implementation in the Java programming language. We hope this blog has helped you enhance your knowledge regarding logical operators and if you want to learn more, check out our articles on Java. Do upvote our blog to help other ninjas grow.