Hello readers, in today’s topic we will discuss precedence and associativity of operator in Java. Previously we also discussed various important topics of Java like Data Types in Java, Operator in Java, Variables & Constant in Java, and many more. So without wasting any time let’s dive into today’s topic:
Operator precedence and associativity in Java
The operator is applied and evaluated based on precedence. For example (+,-) has less precedence compared to (*,/). Hence * & / are evaluated first.
In case we like to change this order we use parenthesis.
Associativity tells the direction of execution of the operator. It can either be left to right or right to left.
* /: Left to Right
+ – : Left to Right
+ + =: Right to Left
It specifies the order through which operators are executed from left to right or right to left.
Resulting data type after an arithmetic operation
The following table summarises the resulting data type after arithmetic operator on them
|R = byte + short = int|
|R = short + int = int|
|R = char + int = int|
|R = char + short = int|
|R = long + float = float|
|R = int + float = float|
|R = long + double = double|
|R = float + double = double|
So this is all about the Operator precedence and associativity in Java. These are the various terms and concepts which are used in a java program. I hope this is helpful to you. In the next upcoming post we will discuss another new topic of java, so wait for this will meet in the next post. Thank you