Expressions consist of operands and operators. Expression operators indicate, which actions to apply to the operands. Examples of operators are summation, subtraction, and so on. Examples of operands are literals, fields, local variables, and expressions.
There are three types of operators:
Unary operators. Unary operators take one operand and use prefix notation (for example, -X).
Binary operators. Binary operators take two operands and use infix notation (for example, X+Y).
Ternary operator. Only one ternary operators exists. This is the selection operation. It takes three operands and uses infix notation: C ? X : Y.
The order of calculating operators in an expression is determined by the precedence and associativity of the operators. Operands in an expression are calculated from left to right.
When an expression contains multiple operators, the precedence of the operations controls, in which order individual operators are calculated. For example, the expression x + y * z is calculated as x + (y * z) because the multiplication operator has higher priority than the summation operator. Operator priority is established by defining corresponding grammar rules. The following table contains all operators in order of precedence from highest to lowest:
Operator category | Operators |
Basic | x.y F(x) a[x] New |
Unary | + - Not |
Multiplicative | * / Div Mod |
Additive | + - |
Type comparison and testing | < > <= >= Is As |
Equality | = <> |
Logical AND | And |
Logical OR | Or Xor |
Selection operator | ?: |
When an operand occurs between two operations with the same precedence, the associativity of the operators controls, in which order operators are executed:
All binary operators are left-associative. These operators are executed from left to right. For example, the expression x + y + z is calculated as (x + y) + z.
Operator precedence and associativity can be controlled using parentheses.
See also: