Java logical operators list with examples

Margaret 145 Published: 08/22/2024

Java logical operators list with examples

I'm sorry! I'll respond in English as you requested.

Here's a comprehensive list of Java logical operators along with their syntax and usage:

Logical AND (&&)

Syntax: a && b (where a and b are boolean expressions)

Example: int x = 5; int y = 10; if(x > 0 && y > 0) System.out.println("Both conditions are true");

The && operator returns true only when both operands evaluate to true. In the above example, the condition will be evaluated as true only when both x and y are greater than zero.

Logical OR (||)

Syntax: a || b (where a and b are boolean expressions)

Example: int x = 5; int y = -10; if(x > 0 || y < 0) System.out.println("At least one condition is true");

The || operator returns true when at least one operand evaluates to true. In the above example, the condition will be evaluated as true when either x is greater than zero or y is less than zero.

Logical NOT (!)

Syntax: !a (where a is a boolean expression)

Example: int x = 5; if(!x > 0) System.out.println("The condition is false");

The ! operator returns the opposite of its operand's value. In the above example, the condition will be evaluated as true when x is not greater than zero.

Logical XOR (^)

Syntax: a ^ b (where a and b are boolean expressions)

Example: int x = 5; int y = -10; if((x > 0)^(y < 0)) System.out.println("Exactly one condition is true");

The ^ operator returns true when exactly one operand evaluates to true. In the above example, the condition will be evaluated as true when either x is greater than zero or y is less than zero, but not both.

Logical EQUIVALENCE (==)

Syntax: a == b (where a and b are boolean expressions)

Example: int x = 5; int y = 5; if(x == y) System.out.println("The values are equal");

The == operator returns true when the operands have the same value. In the above example, the condition will be evaluated as true when both x and y have the value 5.

Logical INEQULAITY (!=)

Syntax: a != b (where a and b are boolean expressions)

Example: int x = 5; int y = -10; if(x != y) System.out.println("The values are not equal");

The != operator returns true when the operands have different values. In the above example, the condition will be evaluated as true when x is greater than zero and y is less than zero.

Logical IMPLICATION (->)

Syntax: a -> b (where a and b are boolean expressions)

Example: int x = 5; if(x > 0 -> true) System.out.println("The implication is true");

The -> operator returns true when the antecedent (a) is false, regardless of the value of the consequent (b). In the above example, the condition will be evaluated as true as long as x is greater than zero.

Logical BICONDITIONAL (<=>)

Syntax: a <= b (where a and b are boolean expressions)

Example: int x = 5; int y = -10; if(x > 0 <= y < 0) System.out.println("The conditions are logically equivalent");

The <=> operator returns true when the operands have the same truth value. In the above example, the condition will be evaluated as true only when both x is greater than zero and y is less than zero.

These logical operators are used to manipulate boolean values in Java programming language. They can be combined using parentheses to create more complex conditions.

Java logical operators list w3schools

I'm happy to help! However, since you mentioned to respond in English and I understand that you want me to provide the list of Java logical operators from W3Schools, I will do so. Here it is:

Java Logical Operators

======================

Java provides several logical operators that can be used to perform various logical operations. The following are the most commonly used logical operators in Java:

1. && (And)

The && operator performs a logical AND operation on two operands. It returns true if both operands are true.

Example:

int x = 10;

int y = 20;

if (x > 5 && y > 15) {

System.out.println("Both conditions are true");

}

2. || (Or)

The || operator performs a logical OR operation on two operands. It returns true if at least one operand is true.

Example:

int x = 10;

int y = 20;

if (x > 5 || y > 15) {

System.out.println("At least one condition is true");

}

3. ! (Not)

The ! operator performs a logical NOT operation on a single operand. It returns true if the operand is false, and vice versa.

Example:

int x = 10;

if (!x > 5) {

System.out.println("Condition is false");

}

4. & (And bit-wise)

The & operator performs a bitwise AND operation on two operands. It returns a value that has bits set to 1 if both corresponding bits in the two operands are 1.

Example:

int x = 5;

int y = 3;

if ((x & 2) == 0) {

System.out.println("Bitwise AND operation returns false");

}

5. ^ (Exclusive Or)

The ^ operator performs an exclusive OR operation on two operands. It returns a value that has bits set to 1 if the corresponding bits in the two operands are different.

Example:

int x = 5;

int y = 3;

if ((x ^ y) == 0) {

System.out.println("Exclusive OR operation returns false");

}

6. ~ (Bit-wise Not)

The ~ operator performs a bitwise NOT operation on a single operand. It returns a value that has bits set to the opposite of those in the original value.

Example:

int x = 5;

if ((~x) == -6) {

System.out.println("Bitwise NOT operation returns correct result");

}

That's it! These are the most commonly used logical operators in Java. You can use these operators to perform various logical operations and control the flow of your program.

Hope this helps!