Cava supports five categories of operators for arithmetic, comparison, bitwise, logical, and assignment operations. This page describes each operator's behavior, supported types, and precedence rules.
Cava does not support the conditional (ternary) operator (?:). Use if statements instead.
Arithmetic operators
All arithmetic operators work with numeric types. Example variables: int left = 20, right = 10.
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
+ | Addition | Adds both operands | left + right | 30 |
- | Subtraction | Subtracts the right operand from the left | left - right | 10 |
* | Multiplication | Multiplies both operands | left * right | 200 |
/ | Division | Divides the left operand by the right | left / right | 2 |
% | Modulo | Returns the remainder of the division | left % right | 0 |
++ | Increment | Adds 1 to the operand | left++ | left becomes 21 |
-- | Decrement | Subtracts 1 from the operand | left-- | left becomes 19 |
Note: The right operand of/cannot be0. A division-by-zero error is triggered if it is.
Post-increment vs. pre-increment: The position of ++ (and --) controls when the increment happens relative to the assignment. int b = a++ assigns a to b first, then increments a. int b = ++a increments a first, then assigns a to b.
Relational operators
Relational operators compare two operands and return a boolean result. Use them in if conditions. Example variables: int left = 20, right = 10.
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
> | Greater than | true if left > right | left > right | true |
< | Less than | true if left < right | left < right | false |
>= | Greater than or equal to | true if left >= right | left >= right | true |
<= | Less than or equal to | true if left <= right | left <= right | false |
== | Equal to | true if both operands are equal | left == right | false |
!= | Not equal to | true if operands differ | left != right | true |
Bitwise operators
Bitwise operators work at the binary level and are supported on INT, LONG, SHORT, CHAR, and BYTE types. Example variables: int left = 1 (binary: 01), right = 2 (binary: 10).
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
& | Bitwise AND | Returns 1 for each bit position where both operands have 1 | left & right (01 & 10) | 0 (00) |
| | Bitwise OR | Returns 1 for each bit position where either operand has 1 | left | right (01 | 10) | 3 (11) |
^ | Bitwise XOR | Returns 1 for each bit position where the operands differ | left ^ right (01 ^ 10) | 3 (11) |
~ | Bitwise NOT | Inverts all bits of the operand | ~left (~01) | -2 |
<< | Left shift | Shifts bits left by the specified number of positions | left << 2 (01 << 2) | 4 (0100) |
>> | Right shift | Shifts bits right by the specified number of positions | right >> 1 (10 >> 1) | 1 (01) |
Logical operators
Logical operators combine boolean expressions. Example variables: boolean left = true, right = false.
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
&& | Logical AND | true if both operands are true | left && right | false |
|| | Logical OR | true if at least one operand is true | left || right | true |
! | Logical NOT | Inverts the boolean value | !left | false |
Assignment operators
Assignment operators assign a value to a variable. Compound assignment operators combine an operation with assignment. Example variables: int left = 1, right = 2.
| Operator | Name | Meaning | Example | left after |
|---|---|---|---|---|
= | Assignment | left = right | left = right | 2 |
+= | Addition assignment | left = left + right | left += right | 3 |
-= | Subtraction assignment | left = left - right | left -= right | -1 |
*= | Multiplication assignment | left = left * right | left *= right | 2 |
/= | Division assignment | left = left / right | left /= right | 0 |
%= | Modulus assignment | left = left % right | left %= right | 1 |
&= | Bitwise AND assignment | left = left & right | left &= right | 0 |
|= | Bitwise OR assignment | left = left | right | left |= right | 3 |
^= | Bitwise XOR assignment | left = left ^ right | left ^= right | 3 |
<<= | Left shift assignment | left = left << right | left <<= right | 4 |
>>= | Right shift assignment | left = left >> right | left >>= right | 0 |
Operator precedence
When an expression contains multiple operators, Cava evaluates them in the following order (highest to lowest precedence).
| Precedence | Name | Operator | Associativity |
|---|---|---|---|
| 1 (highest) | Grouping, member access | () [] . | Left-to-right |
| 2 | Unary, increment, decrement | ! + (unary) - (unary) ~ ++ -- | Right-to-left |
| 3 | Multiplicative | * / % | Left-to-right |
| 4 | Additive | + - | Left-to-right |
| 5 | Shift | << >> | Left-to-right |
| 6 | Relational | < <= > >= | Left-to-right |
| 7 | Equality | == != | Left-to-right |
| 8 | Bitwise AND | & | Left-to-right |
| 9 | Bitwise XOR | ^ | Left-to-right |
| 10 | Bitwise OR | | | Left-to-right |
| 11 | Logical AND | && | Left-to-right |
| 12 | Logical OR | || | Left-to-right |
| 13 (lowest) | Assignment | = += -= *= /= %= &= |= ^= ~= <<= >>= | Right-to-left |
Precedence rules can be non-obvious in complex expressions. Use parentheses () to make the evaluation order explicit:
int a = 1;
int b = 2;
int c = a * (b + 2); // b + 2 is evaluated first, then multiplied by a