Java

Logical operators in Java explained

Java supports all kinds of operators that a normal programming language does. The logical operators in Java are the same as of any other language. The AND, OR, and NOT are referred to as the logical operators in Java. The primary use of logical operators is to combine the conditions and then determine the result by checking the condition with several relations like AND, OR, and NOT. This article demonstrates the working of all the logical operators. Moreover, we have also provided the usage of these operators by practicing a few examples of logical operators.

How logical operators work

This section demonstrates the working mechanism of logical operators. For this, we will break this section into several sub-sections that provide the working according to the types of logical operators.

Logical AND operators (&&)

The logic AND links two conditions together and checks both the conditions. One of the following results are expected using AND operator:

  • true value if both conditions are true
  • returns a false value if both or even one condition is not true

The syntax (showing the relation between condition1 and condition2) to use the AND operator is mentioned below:

condition1 && condition2

Logical OR operator

It also works based on two conditions, The OR operator may return the false or true values as mentioned below:

  • returns a true value if any or each condition is true
  • returns the false value (only) if both conditions are false

The OR(showing OR relation of condition1 and condition2) operator functions on the following syntax:

condtion1 || condition2

Logical NOT operator

The logical NOT operator works differently as compared to other logical operators. The NOT operator considers only one condition (unary operator) and returns true/false values as follows:

  • returns a false value if the condition is true
  • returns a true value if the condition is false

The syntax provided below is followed by NOT operator

!(condition)

After going through this section, you would have learned about the syntax and working of each logical operator.

How to use logical operators

This section provides examples of all types of logical operators.

Logical AND operator

The AND operator is used to return by checking two conditions. For example, the following example practices the AND operator on variables a and b.

As both conditions are true, the if-block of the code is executed:

package lh;

public class loper {
    public static void main(String[]args) {
    //declaring two variables
    int a=5, b=6;
   
    //setting condition
    if ( a>=5 && b==6 )
    {
        System.out.println("Welcome to linuxhint");
    }
    else
    {
        System.out.println("Access denied! Please try again");
    }

}
}

Output

However, if we execute the following code, where one condition is false. You would observe that the else block of the code is executed:

package lh;

public class loper {
    public static void main(String[]args) {
    //declaring two variables
    int a=5, b=6;
   
    //setting condition
    if ( a>=5 && b<6 )
    {
        System.out.println("Welcome to linuxhint");
    }
    else
    {
        System.out.println("Access denied! Please try again");
    }

}
}

Output

Logical OR operator

The OR operator also checks two conditions, the following example demonstrates the use of the OR operator in Java. In the following example, the two variables c and d are checked against the set condition using the if-else statement. It is observed that the “if-block” of the “if-else” statement is executed because one condition is true.

package lh;

public class loper {
    public static void main(String[]args) {
    //declaring two variables
    int c=10, d=12;
   
    //setting condition and using "OR" operator
    if ( c<20 || d<10 )
    {
        System.out.println("Welcome to linuxhint");
    }
    else
    {
        System.out.println("Access denied! Please try again");
    }

}
}

Output

However, in the following code, both the conditions are false therefore the else statement’s is printed:

package lh;

public class loper {
    public static void main(String[]args) {
    //declaring two variables
    int c=10, d=12;
   
    //setting condition and using "OR" operator
    if ( c>10 || d==15 )
    {
        System.out.println("Welcome to linuxhint");
    }
    else
    {
        System.out.println("Access denied! Please try again");
    }

}
}

Output

Logical NOT operator

As discussed earlier, the NOT operator considers only one condition. The example provided below checks prints the if block, although the condition is false, the NOT operator will consider it as true:

package lh;

public class loper {
    public static void main(String[]args) {
    //declaring variables
    int e=10, f=12;
   
    //setting condition and using "NOT" operator
    if (!(e>f))
    {
        System.out.println("Welcome to linuxhint");
    }
    else
    {
        System.out.println("Access denied! Please try again");
    }

}
}

Output

The following code would execute the else block of the if-else statement because the condition is true (as it is used with NOT operator so the condition will be considered as false):

package lh;

public class loper {
    public static void main(String[]args) {
    //declaring variables
    int e=10, f=12;
   
    //setting condition and using "NOT" operator
    if (!(e<f))
    {
        System.out.println("Welcome to linuxhint");
    }
    else
    {
        System.out.println("Access denied! Please try again");
    }

}
}

Output

Conclusion

The logical operators in Java work by checking the conditions and returning the results accordingly. This descriptive post provides the working and usage of several kinds of logical operators. The AND OR and NOT operators belong to the category of logical operators. The AND and OR operators depend on the true or false of two conditions whereas the NOT operator considers only one condition for execution.

About the author

Adnan Shabbir