**Java Operators**

In this chapter we will learn about Operator Precedence and Operator Associativity.

**Operator Precedence**

Precedence decides which operator will be evaluated first in a case where more than one operators are present in the same calculation.

**Operator Precedence Table**

Operators | Precedence(High to Low) |
---|---|

postfix | expr++ expr— |

unary | ++expr —expr +expr –expr ~ ! |

multiplicative | * / % |

additive | + – |

shift | << >> >>> |

relational | < > <= >= instanceof |

equality | == != |

bitwise AND | & |

bitwise exclusive OR | ^ |

bitwise inclusive OR | | |

logical AND | && |

logical OR | || |

ternary | ? : |

assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= |

**Example of Precedence
**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/* * Here we will see the effect of precedence in operators life */ class OperatorPrecedenceExample { public static void main(String args[]) { int i = 40; int j = 80; int k = 40; int l = i + j / k; /* * In above calculation we are not using any bracket. So which operator * will be evaluated first is decided by Precedence. As precedence of * divison(/) is higher then plus(+) as per above table so divison will * be evaluated first and then plus. * * So the output will be 42. */ System.out.println("value of L :" + l); int m = (i + j) / k; /* * In above calculation brackets are used so precedence will not come in * picture and plus(+) will be evaluated first and then divison()/. So * output will be 3 */ System.out.println("Value of M:" + m); } } |

**Operator Associativity**

If two operators having same precedence exists in the calculation then Associativity of the operators will be used to decide which operator will be executed first.

Example of Associativity

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package jbt.bean; /* * Here we will see the effect of precedence in operators life */ public class OperatorAssociativityExample { public static void main(String args[]) { int i = 40; int j = 80; int k = 40; int l = i / k * 2 + j; /* * In above calculation we are not using any bracket. And there are two * operator of same precedence(divion and multiplication) so which * operator(/ or *) will be evaluated first is decided by association. * Associativity of * & / is left to right. So divison will be evaluated * first then multiplication. * * So the output will be 82. */ System.out.println("value of L :" + l); int m = i / (k * 2) + j; /* * In above calculation brackets are used so associativity will not come * in picture and multiply(*) will be evaluated first and then * divison()/. So output will be 80 */ System.out.println("Value of M:" + m); } } |

**Operators in Java**

Let us discuss about each operator individually.Assignment (=) and Arithmetic operators(+, -, *, /) will work the same way as they do in other programming language. So we will not discuss about them. The precedence for ’/‘ & ’*‘ operators is higher than sum(+) or minus(-) or modular division(%).