Operator precedence
Java Operator Precedence
Operators Precedence
postfix increment and decrement ++ --
prefix increment and decrement, and unary ++ -- + - ~ !
multiplicative * / %
additive + -
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ? :
= += -= *= /= %=
assignment &= ^= |= <<= >>= >>>=
1. int x=10;
int y=++x + x++ * x++;
Answer:
* is having higher precedence than +
int y = (++x) + (x++ * x++)
= 11 + (11*12)
= 11 + 132
= 143
x = 13
2. int x = 10;
int y = ++x * x++ + ++x;
Answer:
* is having higher precedence than +
int y = (++x * x++) + (++x)
= (11*11) + (13)
= 121 + 13
= 134
x=13
3. int x = 10;
int y = ++x * x++ / ++x;
Answer:
* and / are having equal precedence but from left
to right in this expression * is coming first
int y = (++x * x++)/++x
= (11 * 11)/13
= 121/13
=9
x=13
4. int x=10
int y = ++x / x++ * x++ + --x - x | x
Answer:
/ and * are having highest precedence than + and -
+ and - are having higher precedence than &(bitwise operators)
int y = 11 / 11 * 12 + 12 - 12 | 12
= ((((11/11) * 12) +12)-12) | 12
= (((1*12) +12)-12) |12
= ((12+12)-12) |12
= (24-12) | 12
= 12|12
= 12
x=12
5. int x=10
int y = x+2-12/6
Answer:
division is having higher priority
+ and - are having equal priority
from left to right + is coming first in this expression
y = (x+2) -(12/6)
= (10+2) -(2)
=10
x=10
6. int x= 10
int z = 6
int y = x | z * x++ - x-- - --x + ++x / x
Answer:
* and / are having higher priority
next is + and -
next is bitwise or
int y = (x) | (((z * x++) - x-- - --x + ++x / x
= 10 | ((6 * 10) - 11 - 9 + (10/10))
= 10 | (60-11-9+1)
= 10 | (49-9+1)
= 10 | 41
= 10 | 41
= 43
convert 10 to binary = 001010
convert 43 to binary = 101011
10|43 = 101011 = convert this to decimal = 43
x = 10
7. int x = 10
int y = ++x / x++ * x-- - x / x+2 * 3
Answer:
* and / are having higher priority
next is + and -
y = 11/11 * 12 - 11 / 11+2*3
= ((11/11) *12) -(11/11) +(2*3)
= (1*12)-1+6
= 11+6
= 17
x=11