In the Java programming language, the precedence of the logical OR
operator (||
) is higher than the precedence of the logical AND
operator (&&
). This means that expressions involving ||
will be evaluated before expressions involving &&
. For example:
boolean a = true; boolean b = false; boolean c = true; System.out.println(a || b && c); // Outputs: true
In the example above, the expression a || b && c
is evaluated as follows:
b && c
is evaluated first because &&
has a lower precedence than ||
. This expression evaluates to false
.a || false
is then evaluated, which evaluates to true
.You can use parentheses to specify the order in which the operators should be applied. For example:
System.out.println((a || b) && c); // Outputs: true
In this case, the expression (a || b) && c
is evaluated as follows:
a || b
is evaluated first because it is enclosed in parentheses. This expression evaluates to true
.true && c
is then evaluated, which also evaluates to true
.It's important to understand operator precedence when writing complex boolean expressions, as it can affect the result of the expression if the operators are not applied in the order you expect.