java and or precedence

https:‮al.www//‬utturi.com
java and or precedence

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:

  1. b && c is evaluated first because && has a lower precedence than ||. This expression evaluates to false.
  2. 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:

  1. a || b is evaluated first because it is enclosed in parentheses. This expression evaluates to true.
  2. 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.

Created Time:2017-11-01 22:29:55  Author:lautturi