Javatpoint Logo
Javatpoint Logo

Types of Logical Operators in Java

In Java, logical operators are used to manipulate boolean values. There are three types of logical operators in Java:

  • AND (&&): The AND operator returns true if both the operands are true, otherwise it returns false. For example:

In the example above, the AND operator returns true because both x is greater than 3 and y is less than 10.

  • OR (||): The OR operator returns true if at least one of the operands is true, otherwise it returns false. For example:

In the example above, the OR operator returns true because y is less than 10, even though x is not greater than 10.

  • NOT (!): The NOT operator is used to reverse the value of a boolean expression. If the expression is true, the NOT operator returns false, and if the expression is false, the NOT operator returns true. For example:

In the example above, the NOT operator reverses the value of x from true to false, so the else block is executed. First, it is important to note that logical operators evaluate their operands from left to right, and stop as soon as the result of the expression can be determined. This means that in an expression such as A && B, if A is false, B will not be evaluated, because the whole expression will already be false.Second, logical operators can also be used with non-boolean expressions, but they will first be converted to boolean values using the following rules:

  • Any non-zero numeric value is considered true.
  • The boolean value true is considered true.
  • The boolean value false is considered false.
  • Any null reference is considered false.

For example, the following code will print "At least one value is non-zero":

In this case, x is 0, which is considered false, but y is 7, which is considered true, so the OR operator returns true. Third, logical operators can be combined in more complex expressions, using parentheses to control the order of evaluation. For example, the following code will print "Both conditions are true":

In this case, the expression (x > 3 && y < 10) is evaluated first, and returns true, because both conditions are true. Then, the expression (z == 3 && x <= 5) is evaluated, and returns false, because x is greater than 5. Finally, the OR operator combines the two results, and returns true, because at least one of the expressions is true. Overall, logical operators are a powerful tool for manipulating boolean values and making decisions in Java programs. It is important to understand their behavior and how to use them effectively in complex expressions.

  • Short-circuit evaluation: Java's logical operators support short-circuit evaluation, which means that if the result of an expression can be determined by evaluating only part of the expression, the rest of the expression will not be evaluated. This can improve performance and prevent errors. For example, consider the following code:

In this case, if str is null, the first condition str != null will be false, and the second condition str.length() > 0 will not be evaluated, because the whole expression will already be false. This prevents a NullPointerException from being thrown when trying to access the length of a null string.

  • Bitwise operators: Java also has bitwise operators, which are used to manipulate the bits of integer values. These operators include AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). Bitwise operators are not logical operators, because they work on integer values rather than boolean values. For example, the following code will print "3":

In this case, x and y are represented in binary as 101 and 110, respectively. The bitwise AND operator compares the corresponding bits of x and y, and sets each bit of the result to 1 if both bits are 1. The result is 100, which represents the decimal value 4.

  • Precedence: Like arithmetic operators, logical operators have a precedence, which determines the order in which they are evaluated. The NOT operator has the highest precedence, followed by AND, then OR. Parentheses can be used to override the default precedence and control the order of evaluation. For example, the following code will print "false":

In this case, the expression inside the parentheses is evaluated first, which returns false, because x is not equal to 6. Then, the NOT operator is applied to the result, which reverses it to true.

  • Ternary operator: Java also has a ternary operator (? :) that can be used as a shorthand for an if-else statement. The ternary operator takes three operands: a boolean expression, a value to be returned if the expression is true, and a value to be returned if the expression is false. For example, the following code will print "x is greater than y":

In this case, the boolean expression x > y is true, so the first operand of the ternary operator is returned, which is the string "x is greater than y".

  • De Morgan's laws: De Morgan's laws are a set of rules that describe how to negate complex boolean expressions. In Java, De Morgan's laws can be used to simplify expressions that involve logical operators. The first law states that the negation of a conjunction (AND) is the disjunction (OR) of the negations. In other words, !(A && B) is equivalent to !A || !B. For example, the following code will print "x is not equal to 3 and y is not equal to 4":

In this case, the expression x == 3 || y == 4 evaluates to false, because neither condition is true. Then, the NOT operator is applied to the result, which makes the whole expression true, according to De Morgan's laws.

  • Logical assignment operators: Java also has logical assignment operators, which combine a logical operator with an assignment operator. The logical assignment operators include AND (&=), OR (|=), and XOR (^=). These operators apply the logical operator to the left-hand side operand and the right-hand side operand, and then assign the result to the left-hand side operand. For example, the following code will print "x is now 7":

In this case, the OR operator is applied to x and the value 2, which sets the second bit of x to 1. The result is 7, which is assigned back to x.

  • Equality and relational operators: In addition to logical operators, Java also has equality and relational operators that can be used to compare values. The equality operators include == (equal to) and != (not equal to), while the relational operators include < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to).For example, the following code will print "x is greater than y":

In this case, the relational operator > is used to compare x and y, and the expression evaluates to true, because x is greater than y.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA