When reading a book or a newspaper, some written languages are evaluated from left to right, while some are evaluated from right to left. In mathematics, certain operators can override other operators and be evaluated first. Determining which operators are evaluated in what order is referred to as operator precedence. In this manner, Java more closely follows the rules for mathematics. Consider the following expression:
var perimeter = 2 * height + 2 * length;
Let’s apply some optional parentheses to demonstrate how the compiler evaluates this statement:
var perimeter = ((2 * height) + (2 * length));
The multiplication operator (*) has a higher precedence than the addition operator (+), so the height and length are both multiplied by 2 before being added together. The assignment operator (=) has the lowest order of precedence, so the assignment to the perimeter variable is performed last.
Unless overridden with parentheses, Java operators follow order of operation, listed in Table 2.1, by decreasing order of operator precedence. If two operators have the same level of precedence, then Java guarantees left-to-right evaluation for most operators other than the ones marked in the table.
TABLE 2 . 1 Order of operator precedence | |||
Operator | Symbols and examples | Evaluation | |
Post-unary operators | expression++, expression— | Left-to-right | |
Pre-unary operators | ++expression, —expression | Left-to-right | |
Other unary operators | -,!, ~, +, (type) | Right-to-left | |
Cast | (Type)reference | Right-to-left | |
Multiplication/division/modulus | *,/,% | Left-to-right | |
Addition/subtraction | +, - | Left-to-right | |
Shift operators | <<, >>, >>> | Left-to-right | |
Relational operators | <, >, <=, >=, instanceof | Left-to-right | |
Equal to/not equal to | ==, != | Left-to-right | |
Logical AND | & | Left-to-right | |
Logical exclusive OR | ^ | Left-to-right | |
Logical inclusive OR | | | Left-to-right | |
Conditional AND | && | Left-to-right | |
Conditional OR | || | Left-to-right | |
Ternary operators | boolean expression ? expression1 : | Right-to-left | |
expression2 | |||
Assignment operators | =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, | Right-to-left | |
>>>= | |||
Arrow operator | -> | Right-to-left | |
We recommend keeping Table 2.1 handy throughout this chapter. For the exam, you need to memorize the order of precedence in this table. Note that you won’t be tested on some operators, like the shift operators, although we recommend that you be aware of their existence.
The arrow operator (->), sometimes called the arrow function or lambda operator, is a binary operator that represents a relationship between
two operands. Although we won’t cover the arrow operator in this chapter, you will see it used in switch expressions in Chapter 3, “Making Decisions,” and in lambda expressions starting in Chapter 8, “Lambdas and Functional Interfaces.”
By definition, a unary operator is one that requires exactly one operand, or variable, to function. As shown in Table 2.2, they often perform simple tasks, such as increasing a numeric variable by one or negating a boolean value.
TABLE 2 . 2 Unary operators
Operator | Examples | Description |
Logical | !a | Inverts a boolean’s logical value |
complement | ||
Bitwise | ~b | Inverts all 0s and 1s in a number |
complement | ||
Plus | +c | Indicates a number is positive, although numbers are |
assumed to be positive in Java unless accompanied by a | ||
negative unary operator | ||
Negation or | -d | Indicates a literal number is negative or negates an |
minus | expression | |
Increment | ++e | Increments a value by 1 |
f++ | ||
Decrement | –f | Decrements a value by 1 |
h– | ||
Cast | (String)i | Casts a value to a specific type |
Even though Table 2.2 includes the casting operator, we postpone discussing casting until the “Assigning Values” section later in this chapter, since that is where it is commonly used.