Understanding Java Operators

A Java operator is a symbol that can be applied to a set of variables, values, or literals — referred to as operands — and that returns a result. Three types of operators are available in Java: unary, binary, and ternary. These types of operators can be applied to one, two or three operands, respectively. You’ll need to know a specific subset of Java operators, how to apply them, and the order in which they should be applied.

Java operators are not necessarily evaluated from left-to-right order. For example, the following java expression is actually evaluated from right-to-left given the specific operators involved:

int x = 4;

In this example, you would first decrement x from 4 to 3, and then multiply the resulting value by 2, and finally add 3. The value would then be automatically upcast from 9 (Integer) to 9.0 (Double) and assigned to y. The final value of x and y would be 3 and 9.0 respectively.

Unless overridden with parentheses, Java operators follow order of operation, listed in Table below, by decreasing order of operator precedence. If two operators have the same level of precedence, then Java guarantees left-to-right evaluation.

Thanks for reading my article. If you like it, please give me a small 👏. Appreciate ❤️