Operators are special symbols used to operate on values. They can be grouped into five types: arithmetic, assignment, comparison, logical, and bitwise operators.
Arithmetic operators
There are four basic arithmetic operators, as well as the modulus operator (%), which is used to obtain the division remainder:
Note that the division sign gives an incorrect result. That’s because it operates on two integer values and will therefore round the result and return an integer. To get the correct value, one of the numbers must be explicitly converted to a floating-point type:
Assignment operators
The second group is the assignment operators—most importantly, the assignment operator itself (=), which assigns a value to a variable.
Combined assignment operators
A common use of the assignment and arithmetic operators is to operate on a variable and then save the result back into that same variable. These operations can be shortened with the combined assignment operators:
Increment and decrement operators
Another common operation is to increment or decrement a variable by one. This can be simplified with the increment (++) and decrement (−−) operators:
Both of these can be used either before or after a variable:
The result on the variable is the same whichever is used. The difference is that the post-operator returns the original value before it changes the variable, while the pre-operator changes the variable first and then returns the value:
Comparison operators
The comparison operators compare two values and return either true or false. They’re mainly used to specify conditions, which are expressions that evaluate to either true or false:
Logical operators
The logical operators are often used together with the comparison operators. Logical and (&&) evaluates to true if both the left and right side are true, and logical or (||) is true if either the left or right side is true. For inverting a Boolean result, there is a logical not (!) operator. Note that for both logical and and logical or, the right-hand side won’t be evaluated if the result is already determined by the left-hand side:
Bitwise operators
The bitwise operators can manipulate individual bits inside an integer. For example, the right shift operator (>>) moves all bits except the sign bit to the right, whereas zero-fill right shift (>>>) moves all bits right including the sign bit:
These bitwise operators have shorthand assignment operators, just like the arithmetic operators:
Operator precedence
Precedence | Operator | Precedence | Operator |
---|---|---|---|
1 | ++ −− ! ~ | 7 | & |
2 | * / % | 8 | ^ |
3 | + − | 9 | | |
4 | << >> >>> | 10 | && |
5 | < <= > >= | 11 | || |
6 | == != | 12 | = op= |
For example, logical and (&&) binds weaker than relational operators, which in turn bind weaker than arithmetic operators:
To avoid having to learn the precedents of all operators and to clarify the intent, you can use parentheses to specify which part of the expression will be evaluated first. Parentheses have the highest precedence of all operators: