Java Building Blocks: Operators

Arithmetic, logical, bitwise, string, and special operators.

Overview

An operator is a token that results in the computation of a value based on one or more input values. For example, the + arithmetic operator, when it appears between 2 numeric-valued expressions, combines those 2 into an expression that results (at runtime) in computing their sum as the value of the combined expression.

Some operators have multiple meanings. Let’s take + as an example once again: As a binary operator (combining 2 expressions, one on either side of the operator) between numeric values, it performs addition; if the value on one side or the other is string-valued, + performs string concatenation. If it is used as a unary operator preceding a numeric-valued expression, it has (essentially) no effect at all—e.g. +x gives the same value as x; we can think of this as the arithmetic identity operator. Finally, if it precedes a numeric literal, + is actually treated as part of the literal value, rather than as an operator.

Arithmetic

The operators in this section take operands of numeric types.

Unary

Operator Description
+ Arithmetic identity operator.
- Arithmetic negation; expression value is the negative of the value to which the operator is applied.
++ Pre-increment; expression value is obtained after increment operation.
++ Post-increment; expression value is obtained before increment operation.
-- Pre-decrement; expression value is obtained after decrement operation.
-- Post-decrement; expression value is obtained before decrement operation.

Binary

Operator Description
+ Addition.
- Subtraction.
/ Division.
* Multiplication.
% Modular division.
= Assignment; value of expression is the value assigned to the variable referenced on left side.
+= Compound assignment, aka augmented assignment, aka assignment augmented with addition.
-= Compound assignment, aka augmented assignment, aka assignment augmented with subtraction.
*= Compound assignment, aka augmented assignment, aka assignment augmented with multiplication.
/= Compound assignment, aka augmented assignment, aka assignment augmented with division.
%= Compound assignment, aka augmented assignment, aka assignment augmented with modular division.

Logical

Unary

Operator Description
! Logical negation.

Binary

Operator Description
| Fully evaluated logical OR.
& Fully evaluated logical AND.
&& Short-circuit logical AND.
|| Short-circuit logical OR.
^ Logical XOR.
= Assignment; value of expression is the value assigned to the variable referenced on left side.
&= Compound assignment, aka augmented assignment, aka assignment augmented with logical AND.
|= Compound assignment, aka augmented assignment, aka assignment augmented with logical OR.
^= Compound assignment, aka augmented assignment, aka assignment augmented with logical XOR.

Bitwise

Unary

Operator Description
~ Bitwise NOT (one’s complement); expression value is the result of “bit-flipping” all bits of the term to the right of the operator.

Binary

Operator Description
| Bitwise OR.
& Bitwise AND.
^ Bitwise XOR.
>> Signed right bit shift.
<< Left bit shift.
>>> Unsigned right bit shift.
&= Compound assignment, aka augmented assignment, aka assignment augmented with bitwise AND.
|= Compound assignment, aka augmented assignment, aka assignment augmented with bitwise OR.
^= Compound assignment, aka augmented assignment, aka assignment augmented with bitwise XOR.
>>= Compound assignment, aka augmented assignment, aka assignment augmented with signed right bit shift.
<<= Compound assignment, aka augmented assignment, aka assignment augmented with left bit shift.
>>>= Compound assignment, aka augmented assignment, aka assignment augmented with unsigned right bit shift.

String

Binary

Operator Description
+ Concatenation.
= Assignment; value of expression is the value assigned to the variable referenced on left side.
+= Compound assignment, aka augmented assignment, aka assignment augmented with concatenation.

Other

Operator Description
?, : Ternary operator, written as a ? b : c. Value of the expression is b if expression a evaluates to true, otherwise value is c.
= Assignment; value of expression is the value assigned to the variable referenced on left side.
instanceof Test of object type; returns true if term on right is an instance of the class on the left or any of its ancestor classes, or an instance of an interface implemented by the class on the left or by any of its superclasses.
(type) Cast to type; casts the reference or primitive value that follows to the specified type.
new Object creation.
[] Array element access.
() Expression grouping.
. Class or instance member access.
() Method invocation.

Precedence

In the table below, operations with lower relative precedence are evaluated before those with higher relative precedence. For operators with the same precedence, evaluation proceeds according to the associativity (left-to-right or right-to-left) of the operators.

The information in this and the subsequent sections is also available, in a slightly different form, in “Java Operators”.

Relative Precedence Operator Description Associativity
1 [] Array index left-to-right
1 () Expression grouping left-to-right
1 . Class or instance member access left-to-right
1 () Method invocation left-to-right
2 ++, -- Postfix increment, postfix decrement (none)
3 +, - Unary plus (arithmetic identity), unary minus (arithmetic negation) right-to-left
3 ++, -- Prefix increment, prefix decrement right-to-left
3 ! Boolean (logical) NOT (negation) right-to-left
3 ~ Bitwise NOT (one’s complement) right-to-left
4 (type) Cast to type right-to-left
4 new Object creation right-to-left
5 *, /, % Multiplication, division, modular division (remainder) left-to-right
6 +, - Addition, subtraction left-to-right
6 + String concatenation left-to-right
7 <<, >>, >>> Left shift, signed right shift, unsigned right shift left-to-right
8 <, <=, >, >= Less than, less than or equal to, greater than, greater than or equal to (none)
8 instanceof Type (class or interface) instance test (none)
9 ==, != Value equality, value inequality left-to-right
9 ==, != Reference (identity) equality, reference inequality left-to-right
10 & Bitwise AND left-to-right
10 & Fully evaluated Boolean (logical) AND left-to-right
11 ^ Bitwise XOR (exclusive OR) left-to-right
11 ^ Boolean (logical) XOR (exclusive OR) left-to-right
12 | Bitwise OR left-to-right
12 | Fully evaluated Boolean (logical) OR left-to-right
13 && Short-circuit Boolean (logical) AND left-to-right
14 || Short-circuit Boolean (logical) OR left-to-right
15 ?: Ternary operation (conditional evaluation) right-to-left1
16 =, *=, /=, %=, +=, -=, <<=, >>=, >>>=, &=, ^=, |= Assignment and compound (augmented) assignment right-to-left

Type promotion

For the arithmetic binary and bitwise binary operators, the compiler performs automatic widening/promotion of operand types, following these steps:

  1. If either operand is double, widen the other operand to double (if it is not already double).
  2. Otherwise, if either operand is float, widen the other operand to float (if it is not already float).
  3. Otherwise, if either operand is long, widen the other operand to long (if it is not already long).
  4. Otherwise, widen both operands to int (if either is not already int).

Assignment as expression

An assignment statement is also an expression, with a resulting value—namely, the value that was assigned. Thus, code like this

int a = 1;
int b = 2;
int c = b = a;

is actually meaningful, albeit potentially confusing. The assignment operator has right-to-left associativity. Therefore, in the above code, the value of a will be assigned to b; the value of that assignment expression is the value assigned (the value of a, which is also the value of b now); that value is then assigned to c. On execution of all 3 lines of the above, a, b, and c all have the value 1.

This applies not only to simple assignment, but to augmented assignment as well. So we might write

int a = 1;
int b = 2;
int c = b += a;

On completion of these lines of code, a has the value 1, b has the value 3, and c has the value 3. (Try to trace through the final statement, from right to left, and see if come up with the same results.)

Finally, the increment and decrement operators can be seen as implicit assignment statements; they also behave in this fashion.

What are the values of a and b after execution of the following?

int a = 10;
int b = a++;

How about this time?

int a = 10;
int b = ++a;

Note that many organizations’ style guides specifically forbid using assignments as expressions in this fashion.

  1. In the case of the ternary operator, the right-to-left association is a bit tricky. Basically, what it means is that an expression like a ? b : c ? d : e will be evaluated as if it were written a ? b : (c ? d : e). Similarly, a ? b ? c : d : e is evaluated identically to a ? (b ? c : d) : e. As a rule, you should avoid writing such chained ternary expressions without using parentheses to reduce potential ambiguity.