πŸŽ‰ Special Offer !    Code: GET300OFF    Flat β‚Ή300 OFF on every Java Course
Grab Deal πŸš€

Logical Operators in Java  


Introduction

  • Logical Operators are used to perform logical operations between two or more boolean expressions and return a boolean value (true or false).
  • Logical operators are explained as below :-
    • && (Logical AND Operator):
      • Purpose: Returns true if both operands (expressions) are true, otherwise returns false.
      • Example:
        boolean a = true, b = false;  
        System.out.println(a && b);  // Output: false
    • || (Logical OR Operator):
      • Purpose: Returns true if at least one of the operands (expressions) is true, otherwise returns false.
      • Example:
        boolean a = true, b = false;  
        System.out.println(a || b);  // Output: true
    • ! (Logical NOT Operator):
      • Purpose: Inverts the value of the boolean expression. If the expression is true, it becomes false, and vice versa.
      • Example:
        boolean a = true;  
        System.out.println(!a);  // Output: false
  • Program :
    public class LogicalOperators
    {
        public static void main(String[] args)
        {
            // Initialize boolean variables
            boolean a = true, b = false, c = true;
    
            // Logical AND (&&)
            System.out.println("Logical AND (a && b): " + (a && b));  // Output: false
            System.out.println("Logical AND (a && c): " + (a && c));  // Output: true
    
            // Logical OR (||)
            System.out.println("Logical OR (a || b): " + (a || b));  // Output: true
            System.out.println("Logical OR (b || c): " + (b || c));  // Output: true
    
            // Logical NOT (!)
            System.out.println("Logical NOT (!a): " + !a);  // Output: false
            System.out.println("Logical NOT (!b): " + !b);  // Output: true
    
            // Using logical operators in a complex condition
            if ((a && b) || (c && !b))
            {
                System.out.println("Condition is true");
            }
            else
            {
                System.out.println("Condition is false");  // Output: Condition is true
            }
    
            // Another example with short-circuit behavior
            if (a && b && (c || !b))
            {
                System.out.println("All conditions met");
            }
            else
            {
                System.out.println("Not all conditions met");  // Output: Not all conditions met
            }
        }
    }
    Output:
    Logical AND (a && b): false
    Logical AND (a && c): true
    Logical OR (a || b): true
    Logical OR (b || c): true
    Logical NOT (!a): false
    Logical NOT (!b): true
    Condition is true
    Not all conditions met
Some Advance Important Points :-
  • Usage with Boolean Data Type:
    • Logical operators (&&, ||, !) are primarily used with boolean values (true or false).
    • Example: boolean a = true, b = false; boolean result = a && b; // false.
  • Short-circuit Behavior:
    • Logical && (AND) and || (OR) operators exhibit short-circuit behavior.
      • AND (&&): If the first operand is false, the second operand is not evaluated because the result is already determined.
      • OR (||): If the first operand is true, the second operand is not evaluated because the result is already determined.
    • Example: boolean result = a && (b = false); β€” here, b will not be assigned false if a is false.
  • Combining Logical Operators:
    • Logical operators can be combined to form complex conditions, making them useful for decision-making and flow control.
    • Example: if ((a && b) || (!a && c)) { // complex condition }.
  • Precedence of Logical Operators:
    • Logical operators have lower precedence than relational and arithmetic operators but higher than assignment operators.
    • Parentheses () should be used for clarity when combining logical operators with other operators.
    • Example: boolean result = a && (b || c);.
  • Can Be Used with Conditions:
    • Logical operators can combine multiple conditions to evaluate complex expressions.
    • Example: if (x > 10 && y < 20) { // execute code }.
  • Result is Always Boolean:
    • Logical operators always return a boolean result (true or false).
    • Example: boolean result = a || b; // result is boolean.
  • Efficiency:
    • Logical operators can be more efficient when combined with short-circuit behavior, as unnecessary conditions are not evaluated.
    • Example: In the expression a && b && c, if a is false, b and c are not evaluated.
  • Works Only with Boolean Expressions:
    • Logical operators cannot be used directly on non-boolean types. They are specifically designed for use with boolean values.
    • Example: int a = 5, b = 10; // a && b is invalid and will cause a compile-time error.
  • Avoiding Boolean Confusion:
    • Logical operators work on boolean values, and confusing them with numerical values (e.g., 1 for true, 0 for false) can lead to logical errors.
    • Always use explicit boolean values (true or false) when using logical operators.
  • NOT on Non-Boolean Data Types:
    • The ! (logical NOT) operator is only applicable to boolean values. Using ! on non-boolean types will result in a compile-time error.
    • Example: int a = 5; // !a will cause a compilation error.
  • Use in Conditional Statements:
    • Logical operators are commonly used in if, while, and for conditions to evaluate multiple expressions and determine the flow of control.
    • Example: if (a > 10 && b < 20) { // condition met }.