Arithmetic expressions in any language comprise operands and operators. For reference, x and y are used as operands having values 10 and 20, respectively. The following table shows the precedence order:
Operator |
Description |
** |
Exponent: Performs exponential (power) calculations on operators |
* |
Multiplication: Multiplies values on either side of the operator |
/ |
Division: Divides the left-hand operand by the right-hand operand |
% |
Modulus: Divides the left-hand operand by the right-hand operand and returns the remainder |
+ |
Addition: Adds values on either side of the operator |
- |
Subtraction: Subtracts the right-hand operand from the left-hand operand |
In an arithmetic expression, generally, the rule of Bracket, Of, Division, Multiplication, Addition, and Subtraction (BODMAS) is followed, and operators have their own precedence order. Exponentiation enjoys a higher precedence order, while addition and subtraction have a lower precedence order.
The decreasing precedence order is as follows:
- Exponent
- Unary negation
- Multiplication, division, modulus
- Addition, subtraction
If operands are of the same data type, then the resulting value is also of that type. However, addition of two int data types can produce a long integer.