Logical Operators

You can figure out the logic between variables or numbers with logical operators. In Java, logical operators are used to do logical actions with boolean numbers. These operators allow you to combine multiple boolean expressions and evaluate their truth values. The following are the Logical operators present in Java:

 

1. Logical AND (&&): 

The logical AND operator (&&) evaluates two boolean expressions or two conditions and returns true if both expressions are true, Among two conditions, if any one of them is false then the result will be false. If the first expression evaluates to false, the second expression is not even evaluated, as the overall result will be false regardless of the second expression’s value.

Example:

public class LogicalOperators {    
   public static void main(String[] args) {      
        boolean isRaining = true;
        boolean hasUmbrella = false;
        // We want to check if the person has an umbrella and it's currently raining
        if (isRaining && hasUmbrella) 
            System.out.println("You can go outside.");
         else 
            System.out.println("Staying inside is preferable.");        
   }
}

Output: 

Staying inside is preferable.

Explanation:

In this example, we have two boolean variables, isRaining, and hasUmbrella. The program checks if both conditions are true: whether it’s currently raining (isRaining is true) and if the person has an umbrella (hasUmbrella is true). Only when both conditions are true, it will print “You can go outside. ” Alternatively, it will produce a printed output “Staying inside is preferable.”

Let’s assume that isRaining is true (it’s raining) and hasUmbrella is not true (the person has no umbrella). Since both conditions are true, the AND operator returns true, and the output will be: “It’s better to stay indoors.”

2.Logical OR(||):

The logical OR operator (||) evaluates two boolean expressions and returns true if at least one of the expressions is true, and false only if both expressions are false. If the first expression evaluates to true, the second expression is not evaluated because the overall result will be true regardless of the second expression’s value.

Example:

public class LogicalOperators{
    public static void main(String[] args) {
        int age = 35;
        int income = 42000;

        // We want to check if the person is either older than 30 or has an income
        // greater than 60000
        if (age > 30 || income > 60000) 
            System.out.println("This person is eligible for a higher credit limit.");
        else 
            System.out.println("The credit limit for this person is already high enough.");
    }
}

Output:

The credit limit for this person is already high enough.

This code checks if the variable “age” is greater than 30 or the variable “income” is greater than 60000. If either condition is true, it will print “This person is eligible for a higher credit limit.” Otherwise, it will print “This person does not qualify for a higher credit limit.

When you run this Java program with the “age” set to 35 and “income” set to 42000, since the age condition is met, but the income condition is true, the OR operator returns true, and the output will be: “This person is eligible for a higher credit limit.”

3. Logical Not(!):

The logical NOT operator is reverse the value of a boolean expression. It requires only one operand and gives back the opposite boolean result. In most programming languages, including Java, the logical NOT operator is represented by the exclamation mark!

public class LogicalOperators{
   public static void main(String[] args) {
       boolean isSunny = true;

       // Using the logical NOT operator to negate the value of isSunny
       boolean isNotSunny = !isSunny;

       System.out.println("Is it sunny? " + isSunny);
       System.out.println("Is it not sunny? " + isNotSunny);
   }
}

Output:

Is it not sunny? false

In this example, we have an int variable age set to 18. We then have a boolean variable isAdult, which checks if the age is greater than or equal to 18. If the person’s age is 18 or older, isAdult will be true, indicating that the person is an adult.

Next, we use the logical NOT operator ! to negate the value of isAdult and store it in a new boolean variable called isNotAdult. So, isNotAdult will be false if the person is an adult (isAdult is true), and true if the person is not an adult (isAdult is false).

Visited 1 times, 1 visit(s) today

Comments are closed.

Close