Arithmetic Operator In Java
Definition of an Arithmetic Operators ?
Arithmetic operators are classified as unary, binary and ternary operators based on the number of operands used. The unary operators requires only one operand (+,-,++,--); Binary operators requires two operands(+,-,*,^,/,% and the ternary operator requires three operands(?).
➦All arithmetic operators compute the result of specific arithmetic operation and returns its result. The arguments are not modified.
➦Arithmetic operators "+" and "-" are used to manipulate pointers by adding or subtracting the numeric value to or from the pointers without generating any exception during overflow of the of the pointer's domain.
➦Arithmetic operators can be overloaded when used with user-defined types to extend the nature of normal operators, thus providing additional functionalities.
Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
| + | Addition | a+b |
| - | Subtraction | a-b |
| * | Multiplication | a*b |
| / | Division | a/b |
| % | Modulus | a%b |
Increment and Decrement Operators
| Operator | Description | Example |
|---|---|---|
| ++ | Post Increment | a++ |
| -- | Post Decrement | a-- |
| ++a | Pre Increment | ++a |
| --a | Pre Decrement | --b |
Relational Operators
| Operator | Description | Example |
|---|---|---|
| == | Is equal to | a==b |
| != | Is not equal to | a!=b |
| > | Greater than | a>b |
| < | Less than | a<b |
| >= | Greater than or equal to | a>=b |
| <= | Less than or equal to | a<=b |
Logical Operators
| Operator | Description |
|---|---|
| && | And operator. if both expressions evaluate to True, result is True. If either expression evaluates to False, result is False |
| || | Or operator. if either or both expressions evaluate to True, result is True |
| ! | Not operator. Performs logical negation on an expression. |
Bitwise Operators
| Operator | Description |
|---|---|
| << | Binary left Shift Operator |
| >> | Binary Right Shift Operator |
| ~ | Binary One's Complement Operator |
| & | Binary AND Operator |
| ^ | Binary XOR Operator |
| | | Binary OR Operator |
Assignment Operators
| Operator | Description |
|---|---|
| = | Assign |
| += | Increments, then assigns |
| -= | Decrements, then assigns |
| *= | Multiplies, then assigns |
| /= | Divides, then assigns |
| %= | Modulus, then assigns |
| <<= | Left shift and assigns |
| >>= | Right shift and assigns |
| &= | Bitwise AND assigns |
| ^= | Bitwise exclusive OR and assigns |
| |= | Bitwise inclusive OR and assigns |
Example :
Output :
num1 + num2: 120 num1 - num2: 80 num1 * num2: 2000 num1 / num2: 5 num1 % num2: 0