Thursday, November 24, 2011

Precedence of Operators in Java

Not every boolean or arithmetic operation needs to be parenthesised in order to make sure that the expression is evaluated correctly. Java has an in-built set of rules concerning the precedence of operators in accordance to calculus (and logic) rules.

In the list below the operators are grouped according to their priority (precedence) in the evaluation process of an expression: Operators which are in the same group have the same priority.The groups are numbered from 1 to 15, where 1 represents the highest precedence and 15 the lowest precedence.
  1. Dot operator, array brackets, method invocation: . [] ()
  2. Postfix operators ++  -- (as in a++, a--)
  3. Prefix operators ++  -- (as in ++a, --a), logical and bitwise not ! ~ and the unary operators + - (as in x=-3 or y=+5)
  4. Type casts () and object creation operator  new
  5. Arithmetic operators % * /
  6. Arithmetic operators: + - and the string concatenation operator +
  7. Bit shift operators : >>  <<  >>> (the unsigned right shift operator)
  8. Relational type comparison: <   >  <=   >= and instanceof
  9. Condition checking operators == !=
  10. Bitwise and: &
  11. Bitwise or |
  12. Conditional and:  &&
  13. Conditional or: ||
  14. Conditional operator ?:
  15. Assignment operators :  =  \=  *=  +=  -=  %=  &=  |=  <<=  >>=  >>>=
Precedence influences the evaluation order in the following way:
  • Operators with higher precedence are evaluated before operators with lower precedence.
  • When operators of equal precedence appear in the same expression binary operators (all groups described above except 2,3,4 and 14,15) are evaluated from left to right. Assignment operators (the groups 2,3,5 and 14,15) are evaluated from right to left.
Example:
      int a = 4, b = 2;
      int c = 0;
      c = ++a % b-- * 2 + 5;
      //
      //(1)++a is evaluated first  => a becomes equal to 5
      //    c = 5 % b-- * 2 +5;
      //(2)b-- is evaluated second => b will become equal to 1 after 
      //    a value will be assigned to c
      //    c = 5 % 2 * 2 + 5
      //(3)% - is evaluated third   => 5 % 2 = 1
      //   c = 1 * 2 + 5
      //(4)* - is evaluated forth   => 1 * 2 = 2
      //   c = 2 + 5
      //(5)+ - is evaluated fifth   => 2 + 5 = 7
      //  c = 7
      //(6)= - is evaluated last => c becomes equal to 7
      //After the expression is evaluated b becomes equal to 1
      System.out.println("a="+a+"\n"+
                         "b="+b+"\n"+
                         "c="+c);
      //Output
      //a=5
      //b=1
      //c=7
Note that the example code above is pretty bad in terms of programming style and it should be regarded only as a demonstration on how the compiler evaluates an expression.

Example:
      String testString1 = "";
      testString1 = 1 + 2 + 3 + "Look at me!";
      //The string concatenation operator +  and the arithmetic +
      //have the same precedence, so the evaluation will start from 
      //left to right:
      //(1)The first + is considered arithmetic =>  1 + 2 = 3
      //testString1 = 3 + 3 + "Look at me!";
      //(2)The second + is also considered arithmetic => 3 + 3 = 6
      //testString1 = 6 + "Look at me!";
      //(3)The third + is considered the concatenation operator,
      //so 6 will be converted to the temporary string which will
      //hold the value "6" => "6" + "Look at me!" = "6Look at me!"
      //testString1 = "6" + "Look at me!" = "6Look at me!";
      //(4)The assignment operator will be evaluated last =>
      //testString1 will contain the value "6Look at me!"
      //String testString2 = "";
      testString2 = "Look at me!" + 1 + 2 + 3;
      //The string concatenation operator +  and the arithmetic +
      //have the same precedence, so the evaluation will start
      //from left to right:
      //(1)The first + is considered as being a concatenation
      //operator and the second operand 1 will be converted to the
      //string "1" => "Look at me!"+"1" = "Look at me1"
      //testString2 = "Look at me!" + "1" + 2 + 3 =
      //            = "Look at me!1" + 2 + 3
      //(2)The second + is also considered as being a concatenation
      //operator the the second operand 2 will be converted to the
      //string "2" => "2" + "Look at me1" = "Look at me12"
      //testString2 = "Look at me!1" + 2 + 3 =
      //            = "Look at me!12" + 3
      //(3)The third + is also considered as being a concatenation
      //operator the the second operand 3 will be converted to the
      //string "3" => "3" + "Look at me12" = "Look at me123"
      //testString2 = "Look at me!12" + "3" =
      //     = "Look at me!123"
      //(4)The assignment operator will be evaluated last =>
      //testString1 will contain the value "Look at me!123" */
      String testString3 = "";
      testString3 = "Look at me!" + (1 + 2 + 3);
      //First will be evaluated what is in the round parenthesis
      //from left to right:
      //(1)The first + is considered as being arithmetic => 
      //1 + 2 = 3
      //testString3 = "Look at me!" + (3 + 3);
      //(2)The second + is considered as being arithmetic also =>
      //3 + 3 = 6
      //testString3 = "Look at me!" + 6;
      //(3)After the parenthesis was evaluated, the remaining + will
      //be considered as a concatenation operator:
      //testString3 = "Look at me!" + "6" = "Look at me!6"
      //(4)The assignment operator will be evaluated last =>
      //testString1 will contain the value "6Look at me!"*/
      System.out.println(testString1 + "\n" +
                         testString2 + "\n" +
                         testString3 + "\n");
      //Output
      //6Look at me!
      //Look at me!123
      //Look at me!6
Note:
Because the current syntax highliter I'm using seems to have a problem with /* */ comments (it doesn't recognize */,  so it will highlight everything), I had to use // for each line.

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger