Logical Operators in Java

A logical operator (sometimes called a "Boolean operator") in Java programming is an operator that returns a Boolean result that's based on the Boolean result of one or two other expressions. Sometimes, expressions that use logical operators are called "compound expressions" because the effect of the logical operators is to let you combine two or more condition tests into a single expression. Logical operators when we test more than one condition to make decisions. These are: && (meaning logical AND), || (meaning logical OR) and ! (meaning logical NOT).

&& (Logical AND) expression1 && expression2 true only if both expression1 and expression2 are true
|| (Logical OR) expression1 || expression2 true if either expression1 or expression2 is true
! (Logical NOT) !expression true if expression is false and vice versa

This Java program demonstrates the logical AND (&&) and OR (||) operators. It defines two integer variables m1 and m2 with values of 25 and 75, respectively. The program then uses the logical AND (&&) operator to check if both m1 and m2 are greater than or equal to 35. If both conditions are true, it prints "And && : true". Otherwise, it prints "And && : false".

Next, the program uses the logical OR (||) operator to check if either m1 or m2 is greater than or equal to 35. If either condition is true, it prints "Or || : true". Otherwise, it prints "Or || : false". Since m1 is less than 35 and m2 is greater than 35, the first condition in the logical AND (&&) expression evaluates to false, so the overall result is false. However, since m2 is greater than 35, the second condition in the logical OR (||) expression evaluates to true, so the overall result is true.

Source Code

public class Logical {
    public static void main(String args[])
        int m1=25,m2=75;
        System.out.println("And  &&  : "+(m1>=35 && m2>=35));
        System.out.println("Or   ||  : "+(m1>=35 || m2>=35));


And  &&  : false
Or   ||  : true
To download raw file Click Here

Basic Programs