An expression consists of operands and operators. Operands are normally variable names or literals while operators act on operands. The following are examples of expressions:
int numberWheels = 4; System.out.println("Hello"); numberWheels = numberWheels + 1;
There are several ways of classifying operators:
Arithmetic
Assignment
Relational
Logical complement
Logical
Conditional
Bitwise
Expressions can be thought of as the building blocks of a program. They are used to express the logic of the program.
Java operators are summarized in the following precedence and associativity table. Most of these operators are straightforward:
Precedence |
Operator |
Associativity |
Operator |
---|---|---|---|
1 |
|
Right |
Pre/post increment |
|
Right |
Pre/post decrement | |
|
Right |
Unary plus or minus | |
|
Right |
Bitwise complement | |
|
Right |
Logical complement | |
(cast) |
Right |
Cast | |
2 |
|
Left |
Multiplication, division, and modulus |
3... |