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.
- Dot operator, array brackets, method invocation: . [] ()
- Postfix operators ++ -- (as in a++, a--)
- Prefix operators ++ -- (as in ++a, --a), logical and bitwise not ! ~ and the unary operators + - (as in x=-3 or y=+5)
- Type casts () and object creation operator new
- Arithmetic operators % * /
- Arithmetic operators: + - and the string concatenation operator +
- Bit shift operators : >> << >>> (the unsigned right shift operator)
- Relational type comparison: < > <= >= and instanceof
- Condition checking operators == !=
- Bitwise and: &
- Bitwise or |
- Conditional and: &&
- Conditional or: ||
- Conditional operator ?:
- 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
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!