Java Basics: Conditional Operators

The previous article covered comparison operators, and this article covers conditional operators.

overview

Conditional operators are like we make decisions in real life, it controls the flow of the program and produces results based on the conditions provided.

There are three types of conditional operators namely conditional and, conditional or and ternary operator, let us understand how these conditional operators are used in Java.

The following operators are provided as conditional operators whose operands take values ​​of type Boolean:

operator input example explain
&& x && y Returns true if both x and y are true.
|| x || is Returns true if x or y is true.
& x&y Returns true if both x and y are Boolean and both are true. If both variables are of integer type, a bitwise AND operation is performed.
| x | y Returns true if x and y are both Boolean values ​​and one of them is true. If both variables are of integer type, a bitwise OR operation is performed.
^ x^y Returns true if both x and y are Boolean and x and y are distinct values. If both variables are of integer type, a bitwise XOR operation is performed.
!X Returns true if x is false.

Code example:

package com.test.javaroads.operator;

/**
 * @author: javaroads
 * @date: 2022/12/4 17:43
 * @description:
 */
public class Seven {

    public static void main(String[] args) {
        int a = 0;
        int b = 0;

        if ((!(a > 0)) && (!(b > 0))) {
            a = 10;
            b = 20;
        }

        if ((a > 15) ^ (b > 15)) {
            System.out.println("运算符 = 成立。");
        } else {
            System.out.println("运算符 = 不成立。");
        }
    }
}

Results of the:

Code description:

  • The expression inside the if block is executed if both (!(a > 0)) and (!(b > 0)) are true. Since neither a nor b is greater than 0 (a > 0), (b > 0) returns false and the ! operator returns both (!(a > 0)) and (!(b > 0)) are considered true
  • The expression inside the if block is executed if either (a > 15) or (b > 15) is true. (a > 15) returns false and (b > 15) returns true, so both are true.

The difference between &&, || and &, |

The operators in &&, || and &, | have the same meaning, but their behavior is significantly different. & returns true if both the left and right operands are true, and returns false if the left operand is false, regardless of whether the right operand is true or false. && returns false without executing the right operand if the left operand is false, and returns false after executing the right operand if the left operand is false.

|| and | return true if either the left operand or the right operand is true, which means that if the left operand is true, it returns true regardless of whether the right operand is true or false. || If the left operand is true, returns true without executing the right operand. | Returns true after executing the right operand if the left operand is true.

The behavior difference between &&, || and &, | is whether to execute the right operand when the overall execution result can only be determined by the result of the left operand. Because of these properties, the && and || operators are also known as short-circuit operators .

If only true or false is judged, there is no difference in the execution result no matter which operator is used, but when performing some processing on the right operand, you need to pay attention to the difference in behavior.

Let's take a look at the code below!

package com.test.javaroads.operator;

/**
 * @author: javaroads
 * @date: 2022/12/4 17:51
 * @description:
 */
public class Eight {

    public static void main(String[] args) {
        int a = 5;
        if (a > 0 || (a *= 10) > 100) {
            System.out.println("a是" + a);
        }

        int b = 5;
        if (b > 0 | (b *= 10) > 100) {
            System.out.println("b是" + b);
        }
    }
}

Results of the:

Code explanation:

  • For the || operator, if the left operand is true, the right operand is not executed. , the right operand, which multiplies variable a by 10, is not processed when the println statement is executed because the left operand is true.
  • For the | operator, the right operand is executed even if the left operand is true. Multiply the variable b of the right operand by 10 and execute the println statement.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132236173