conditional && and || and Relational Operators


The conditional-and and conditional-or Operators ( && and || )

Java provides a conditional-and and a conditional-or operator, that both take one or two operands of type boolean and produce a boolean result. These are:

  • && - the conditional-AND operator,
  • || - the conditional-OR operators. The evaluation of <left-expr> && >right-expr< is equivalent to the following pseudo-code:
{
    boolean L = evaluate(<left-expr>);
    if (L) {
        return evaluate(<right-expr>);
    } else {
        // short-circuit the evaluation of the 2nd operand expression
        return false;
    }
}

The evaluation of <left-expr> || >right-expr< is equivalent to the following pseudo-code:

{
    boolean L = evaluate(<left-expr>);
    if (!L) {
        return evaluate(<right-expr>);
    } else {
        // short-circuit the evaluation of the 2nd operand expression
        return true;
    }
}

As the pseudo-code above illustrates, the behavior of the short-circuit operators are equivalent to using if / else statements.

Example - using && as a guard in an expression

The following example shows the most common usage pattern for the && operator. Compare these two versions of a method to test if a supplied Integer is zero.

public boolean isZero(Integer value) {
    return value == 0;
}
 
public boolean isZero(Integer value) {
    return value != null && value == 0;
}

The first version works in most cases, but if the value argument is null, then a NullPointerException will be thrown.

In the second version we have added a "guard" test. The value != null && value == 0 expression is evaluated by first performing the value != null test. If the null test succeeds (i.e. it evaluates to true) then the value == 0 expression is evaluated. If the null test fails, then the evaluation of value == 0 is skipped (short-circuited), and we don't get a NullPointerException.

Example - using && to avoid a costly calculation

The following example shows how && can be used to avoid a relatively costly calculation:

public boolean verify(int value, boolean needPrime) {
    return !needPrime | isPrime(value);
}
 
public boolean verify(int value, boolean needPrime) {
    return !needPrime || isPrime(value);
}

In the first version, both operands of the | will always be evaluated, so the (expensive) isPrime method will be called unnecessarily. The second version avoids the unnecessary call by using || instead of |.


The Relational Operators (<, <=, >, >=)

The operators <, <=, > and >= are binary operators for comparing numeric types. The meaning of the operators is as you would expect. For example, if a and b are declared as any of byte, short, char, int, long, float, double or the corresponding boxed types:

  • 'a < b' tests if the value of 'a' is less than the value of 'b'.
  • 'a <= b' tests if the value of 'a' is less than or equal to the value of 'b'.
  • 'a > b' tests if the value of 'a' is greater than the value of 'b'.
  • 'a >= b' tests if the value of 'a' is greater than or equal to the value of 'b'.

The result type for these operators is boolean in all cases.

Relational operators can be used to compare numbers with different types. For example:

int i = 1;
long l = 2;
if (i < l) {
    System.out.println("i is smaller");
}

Relational operators can be used when either or both numbers are instances of boxed numeric types. For example:

Integer i = 1; // 1 is autoboxed to an Integer
Integer j = 2; // 2 is autoboxed to an Integer
if (i < j) {
    System.out.println("i is smaller");
}

The precise behavior is summarized as follows:

  • If one of the operands is a boxed type, it is unboxed.
  • If either of the operands now a byte, short or char, it is promoted to an int.
  • If the types of the operands are not the same, then the operand with the "smaller" type is promoted to the "larger" type.
  • The comparison is performed on the resulting int, long, float or double values.

You need to be careful with relational comparisons that involve floating point numbers:

  • Expressions that compute floating point numbers often incur rounding errors due to the fact that the computer floating-point representations have limited precision.
  • When comparing an integer type and a floating point type, the conversion of the integer to floating point can also lead to rounding errors.

Finally, Java does bit support the use of relational operators with any types other than the ones listed above. For example, you cannot use these operators to compare strings, arrays of numbers, and so on.

Basic Programs