What logical operator has higher precedence than the other logical operators?

Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator.

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Most Java operators are left-to-right associative. One notable exception is the assigment operator, which is right-to-left associative. As a result, the expression x = y = z = 17 is treated as x = (y = (z = 17)), leaving all three variables with the value 17. Recall that an assignment statement evaluates to the value of its right-hand side. Associativity it not relevant for some operators. For example, x <= y <= z and x++-- are invalid expressions in Java.

Precedence and associativity of Java operators.

The table below shows all Java operators from highest to lowest precedence, along with their associativity. Most programmers do not memorize them all, and, even those that do, use parentheses for clarity.


LevelOperatorDescriptionAssociativity16()
[]
.parentheses
array access
member accessleft-to-right15++
--unary post-increment
unary post-decrementleft-to-right14+
-
!
~
++
--
unary plus
unary minus
unary logical NOT
unary bitwise NOT
unary pre-increment
unary pre-decrementright-to-left13()
newcast
object creationright-to-left12* / %multiplicativeleft-to-right11+ -
+additive
string concatenationleft-to-right10<< >>
>>>shiftleft-to-right9< <=
> >=
instanceofrelationalleft-to-right8==
!=equalityleft-to-right7&bitwise ANDleft-to-right6^bitwise XORleft-to-right5|bitwise ORleft-to-right4&&logical ANDleft-to-right3||logical ORleft-to-right2?:ternaryright-to-left1 =   +=   -=
*=   /=   %=
&=   ^=   |=
<<=  >>= >>>=assignmentright-to-left0->
lambda expression arrowright-to-left


You'll find different (and usually equivalent) operator precedence tables on the web and in textbooks. They typically disagree in inconsequential ways because some operators cannot share operands, so their relative precedence order does not matter (e.g, new and !). There is no explicit operator precedence table in the Java Language Specification. Instead, the operator precedence and associativity rules are inferred via the grammar that defines the Java language.

Order of operand evaluation in Java.

Associativity and precedence determine in which order Java groups operands and operators, but it does not determine in which order the operands are evaluated. In Java, the operands of an operator are always evaluated left-to-right. Similarly, argument lists are always evaluated left-to-right. So, for example in the expression A() + B() * C(D(), E()), the subexpressions are evaluated in the order A(), B(), D(), E(), and C(). Although, C() appears to the left of both D() and E(), we need the results of both D() and E() to evaluate C(). It is considered poor style to write code that relies upon this behavior (and different programming languages may use different rules).

Short-circuit evaluation. With three exceptions (&&,

System.out.println(1 + 2 + "abc");
System.out.println("abc" + 1 + 2);
0, and
System.out.println(1 + 2 + "abc");
System.out.println("abc" + 1 + 2);
1), Java evaluates every operand of an operator before the operation is performed. For the logical AND (&&) and logical OR (
System.out.println(1 + 2 + "abc");
System.out.println("abc" + 1 + 2);
0) operators, Java evaluate the second operand only if it is necessary to resolve the result. This is known as short-circuit evaluation. It allows statements like if ((s != null) && (s.length() < 10)) to work reliably (i.e., invoke the
System.out.println(1 + 2 + "abc");
System.out.println("abc" + 1 + 2);
4 method only if
System.out.println(1 + 2 + "abc");
System.out.println("abc" + 1 + 2);
5 is not
System.out.println(1 + 2 + "abc");
System.out.println("abc" + 1 + 2);
6). Programmers rarely use the non short-circuiting versions (& and |) with boolean expressions.

Operator precedence gone awry.

Sometimes the precedence order defined in a language do not conform with mathematical norms. For example, in Microsoft Excel,
System.out.println(1 + 2 + "abc");
System.out.println("abc" + 1 + 2);
7 is treated as
System.out.println(1 + 2 + "abc");
System.out.println("abc" + 1 + 2);
8 instead of
System.out.println(1 + 2 + "abc");
System.out.println("abc" + 1 + 2);
9. So
year % 4 == 0 && year % 100 != 0 || year % 400 == 0
0 evaluates to
year % 4 == 0 && year % 100 != 0 || year % 400 == 0
1 instead of
year % 4 == 0 && year % 100 != 0 || year % 400 == 0
2, which is the value that most mathematicians would expect. Microsoft acknowledges this quirk as a “design choice.” One wonders whether the programmer was relying on the C precedence order in which unary operators have higher precedence than binary operators. This rule agrees with mathematical conventions for all C operators, but fails with the addition of the exponentiation operator. Once the order was established in Microsoft Excel 2.0, it could not easily be changed without breaking backward compatibility.

Operator associativity gone awry.

Sometimes the associativty of an operator is implemented left-to-right in one programming language but right-to-left in another. An alarming example is exponentiation. In Wolfram Alpha and Google Sheets, the exponentiation operator is right-to-left associative, so
year % 4 == 0 && year % 100 != 0 || year % 400 == 0
3 is treated as
year % 4 == 0 && year % 100 != 0 || year % 400 == 0
4, which is 256. However, in Matlab and Excel, the exponentiation operator is left-to-right associative, so
year % 4 == 0 && year % 100 != 0 || year % 400 == 0
3 is treated as as
year % 4 == 0 && year % 100 != 0 || year % 400 == 0
6, which is 64. Exponentiation is not a binary operator in Java.

Exercises.

  1. What is the result of the following code fragment? Explain.
    System.out.println("1 + 2 = " + 1 + 2);
    System.out.println("1 + 2 = " + (1 + 2));
    
    Answer: 1 + 2 = 12 and 1 + 2 = 3, respectively. If either (or both) of the operands of the + operator is a string, the other is automatically cast to a string. String concatenation and addition have the same precedence and are left-to-right associative. The parentheses in the second statement ensures that the second + operator performs addition instead of string concatenation.
  2. What does the following code fragment print?
    System.out.println(1 + 2 + "abc");
    System.out.println("abc" + 1 + 2);
    

    Answer: 3abc and abc12, respectively. The + operator is left-to-right associative, whether it is string concatenation or addition.

  3. Add parentheses to the following expression to make it more clear to other programmers.
    year % 4 == 0 && year % 100 != 0 || year % 400 == 0
    
    Answer: LeapYear.java shows a variety of equivalent expressions, including the following reasonable alternative:
    ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)
    
  4. What is the value of the expression
    year % 4 == 0 && year % 100 != 0 || year % 400 == 0
    
    7? Explain.

    Answer: It leads to a compile-time error. Java parses it as

    boolean a = false;
    boolean b = false;
    boolean c = true;
    System.out.println(a == b == c);
    
    4. The
    boolean a = false;
    boolean b = false;
    boolean c = true;
    System.out.println(a == b == c);
    
    5 term increments the variable
    boolean a = false;
    boolean b = false;
    boolean c = true;
    System.out.println(a == b == c);
    
    3 and evaulates to the integer
    boolean a = false;
    boolean b = false;
    boolean c = true;
    System.out.println(a == b == c);
    
    7. This leads to a compile-time error becasue you the pre-increment operator must be applied to a variable (not an integer). So, while the pre-increment operator has right-to-left associativity, this property is not useful.

    What logical operator has the highest precedence?

    Logical operators have operator precedence the same as other operators (relational, arithmetic, etc.). The highest precedence belongs to not , followed by and , and finally by or . Like other operations, grouping takes precedence, so we must evaluate bracketed expressions first, if they exist.

    Which of the following operators has higher precedence from other operators?

    Which of the following operators has the highest precedence? Explanation: Unary operators have max precedence in over all other arithmetic operators.

    What has the highest precedence above all operators?

    Explanation: Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator. Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom.