Skip to content
On this page

Boolean Operations

For the Boolean type boolean , there are always only two values: true and false .

Boolean operations are a type of relational operations, including the following categories:

  • Comparison operators: > , >= , < , <= , == , !=
  • AND operation &&
  • OR operation ||
  • Not arithmetic !

Here are some examples:

java
boolean isGreater = 5 > 3; // true
int age = 12;
boolean isZero = age == 0; // false
boolean isNonZero = !isZero; // true
boolean isAdult = age >= 18; // false
boolean isTeenager = age >6 && age <18; // true

The precedence of relational operators from high to low is:

  • !
  • >, >= , < , <=
  • == , !=
  • &&
  • ||

Short Circuit Operation

An important feature of Boolean operations is short-circuit operations. If the result of a Boolean expression can be determined in advance, subsequent calculations will not be performed and the result will be returned directly.

Because the result of false && x is always false , regardless of whether x is true or false , the AND operation does not continue the calculation after determining that the first value is false , but directly returns false .

Let's examine the following code:

java
public class Main {
    public static void main(String[] args) {
        boolean b = 5 < 3;
        boolean result = b && (5 / 0 > 0); // Here 5 / 0 will not report an error
        System.out.println(result);
    }
}

If there is no short-circuit operation, the expression after && will report an error because the divisor is 0 , but in fact the statement does not report an error because the AND operation is a short-circuit operator and the result false is calculated in advance.

If the value of variable b is true , the expression becomes true && (5 / 0 > 0) . Because short-circuit operations cannot be performed, this expression will definitely report an error because the divisor is 0 You can test it yourself.

Similarly, for the || operation, as long as the first value can be determined to be true , subsequent calculations will not be performed, but true will be returned directly:

java
boolean result = true || (5 / 0 > 0); // true

Ternary Operator

Java also provides a ternary operator b ? x : y , which returns the calculation result of one of the subsequent two expressions based on the result of the first Boolean expression. Example:

java
public class Main {
    public static void main(String[] args) {
        int n = -100;
        int x = n >= 0 ? n : -n;
        System.out.println(x);
    }
}

The meaning of the above statement is to determine whether n >= 0 is true. If it is true , return n , otherwise return -n . This is actually an expression that finds the absolute value.

Note that the ternary operation b ? x : y will first calculate b . If b is true , only x will be calculated. Otherwise, only y will be calculated. Furthermore, x and y must be of the same type, since the return value is not boolean , but one of x and y .

Practise

java
public class Main {
    public static void main(String[] args) {
        int age = 7;
        // Definition of primary student: 6~12 years old
        boolean isPrimaryStudent = ???;
        System.out.println(isPrimaryStudent ? "Yes" : "No");
    }
}

Summary

The AND operation and the OR operation are short-circuit operations;

The types behind the ternary operation b ? x : y must be the same. The ternary operation is also a "short-circuit operation" and only x or y is calculated.

Boolean Operations has loaded