DEV Community

Cover image for Associativity and Precedence of Operators in Java
2 2

Associativity and Precedence of Operators in Java

Precedence meaning - the right that somebody/something has to come before somebody/something else because he/she/it is more important. definition comes when you google it.

Precedence in Java means the operators are applied and evaluated based on precedence. for example (+,-) has less procedure compared to (*,!). hence * & / are calculated first.

In case if you want to change this order, you can use parenthesis.

In Java the original common BODMAS Rule doesn't work instead precedence and associativity works. when precedence of is same like * and / then associativity works.

B – Brackets
O – Order of powers or roots,
D – Division
M – Multiplication
A – Addition
S – Subtraction.

The BODMAS rule state that mathematical operations need to be solved from left to right in the order of BODMAS.

Whenever you start working on solving operators problem, first check the operators who has highest precedence value and then start solving but if you encounter operators which has same precedence value than check the associativity rule then start solving.

Source: codeharry

Associativity tells the direction of the execution of operators. It can either be left to right or vice versa.

    / * -> Left to Right

    + - -> Left to Right

    ++, = -> Right to Left
Enter fullscreen mode Exit fullscreen mode


int a = 7*5-34/2;

         /* calculation
         =35-34/2
          =35-17
       output   =18 */


Enter fullscreen mode Exit fullscreen mode

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay