Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Logical Operators (&&, || And !)

Logical operators are used to perform logical operations on boolean expressions. They allow you to combine, negate, or compare boolean values in various ways. Java provides three main logical operators: AND (&&), OR (||), and NOT (!). This tutorial will cover the usage and behavior of these logical operators in Java.

  • Logical AND Operator (&&):

The logical AND operator (&&) returns true if both operands are true; otherwise, it returns false.

boolean result = true && true; // result is true
result = true && false;        // result is false
result = false && true;        // result is false
result = false && false;       // result is false

Note that the logical AND operator is short-circuiting: if the first operand is false, the second operand is not evaluated, as the result is guaranteed to be false.

  • Logical OR Operator (||):

The logical OR operator (||) returns true if at least one of the operands is true; otherwise, it returns false.

boolean result = true || true;   // result is true
result = true || false;          // result is true
result = false || true;          // result is true
result = false || false;         // result is false

Similar to the logical AND operator, the logical OR operator is also short-circuiting: if the first operand is true, the second operand is not evaluated, as the result is guaranteed to be true.

  • Logical NOT Operator (!):

The logical NOT operator (!) returns true if the operand is false and false if the operand is true. In other words, it negates the boolean value of the operand.

boolean result = !true;   // result is false
result = !false;          // result is true
  • Combining logical operators:

You can combine logical operators to create more complex boolean expressions. Parentheses can be used to control the order of evaluation.

boolean a = true;
boolean b = false;
boolean c = true;

boolean result = a && b || c;       // result is true
result = a && (b || c);             // result is true
result = (a && b) || c;             // result is true
result = !(a && (b || c));          // result is false

This tutorial covered the basics of logical operators in Java, including the logical AND (&&), logical OR (||), and logical NOT (!) operators. These operators allow you to combine, negate, or compare boolean values in your code, enabling you to create complex boolean expressions and implement conditional logic.

  1. Using && (logical AND) in Java:

    • The && operator in Java is used for logical AND. It returns true if both operands are true, otherwise false.
    boolean condition1 = true;
    boolean condition2 = false;
    boolean result = condition1 && condition2; // result is false
    
  2. || (logical OR) operator in Java examples:

    • The || operator performs logical OR. It returns true if at least one operand is true.
    boolean condition1 = true;
    boolean condition2 = false;
    boolean result = condition1 || condition2; // result is true
    
  3. ! (logical NOT) operator usage in Java:

    • The ! operator negates the boolean value. It returns true if the operand is false, and vice versa.
    boolean condition = true;
    boolean result = !condition; // result is false
    
  4. Combining logical operators in Java expressions:

    • You can combine logical operators to create complex conditions.
    boolean condition1 = true;
    boolean condition2 = false;
    boolean condition3 = true;
    boolean result = condition1 && (condition2 || condition3); // result is true
    
  5. Short-circuiting behavior of && and || in Java:

    • Java's && and || operators exhibit short-circuiting behavior. If the result can be determined by evaluating only the first operand, the second operand is not evaluated.
    boolean condition1 = false;
    boolean condition2 = true;
    boolean result = condition1 || (condition2); // condition2 is not evaluated
    
  6. Complex conditions with nested logical operators in Java:

    • Nesting logical operators allows you to create intricate conditions.
    int number = 5;
    boolean result = (number > 0 && number < 10) || (number < -5);
    // result is true if the number is between 0 and 10 (exclusive) or less than -5