With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

6.2. Ensuring Correct Associativity

A convention that arises especially in arithmetic relates to associativity. Conventionally, multiplication, division, addition, and subtraction “associate” to the left. A parser must evaluate the leftmost operation first for operations of the same precedence. For example,

System.out.println(25 – 16 - 9);

prints 0. If Java calculated the rightmost subtraction first, the answer would instead be 18.

Exponentiation, on the other hand, conventionally associates to the right. Associating to the right means that a parser must evaluate the rightmost exponentiation first. Thus, 5^3^2 is equal to 5^9, or 1953125. If exponentiation associated to the left, like subtraction, then 5^3^2 would equal 125^2, or 15625. Java ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required