Operators are symbols which is used to do some operations. For example if our operation is addition, we use + symbol and that is our operator. In any operation we have operator and operand. Look at this example:
A+B : Here, A and B are operator and + is operand. Whole expression is called operation.
Types of Operators in Java
- Unary Operators
- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Shift Operators
- Logical Operators
- Ternary Operator
- Assignment Operators
Unary Operators
Those operators which needs only one operand are called unary operators. Click here to know how unary operator works.
| Operator | Name | Description | Example |
|---|---|---|---|
| ++ | Increment | Increases value by 1 | a++, ++a |
| -- | Decrement | Decreases value by 1 | a--, --a |
Arithmetic Operators
Those operators which are used to do basic math operations or arithmetic operations are called arithmetic operators.
| Operator | Name | Description | Example |
|---|---|---|---|
| + | Addition | Adds two values | a + b |
| - | Subtraction | Subtracts one value from another | a - b |
| * | Multiplication | Multiplies two values | a * b |
| / | Division | Divides one value by another | a / b |
| % | Modulus | Returns the division remainder | a % b |
Relational Operators
Those operators which are used to compare two values are called relational operators.
| Operator | Name | Example |
|---|---|---|
| > | Greater than | a > b |
| < | Less than | a < b |
| >= | Greather than or equal to | a >= b |
| <= | Less than or equal to | a <= b |
| == | Equal to | a == b |
| != | Not equal to | a != b |
Bitwise Operators
These operators converts each value in binary and does logical operation in bits. Click here to know how bitwise operator works.
| Operator | Name | Example |
|---|---|---|
| & | AND | a & b |
| | | OR | a | b |
| ~ | NOT | a ~ b |
| ^ | XOR | a ^ b |
Shift Operators
These operators converts each value in binary and shift bits left or right. Click here to know how shift operator works.
| Operator | Name | Example | Same as |
|---|---|---|---|
| << | Left shift | a << b | a * 2 ^ b |
| >> | Right shift | a >> b | a / 2 ^ b |
Logical Operators
Those operators which are used to determine the logic between variables or values are called logical operators.
| Operator | Name | Description | Example |
|---|---|---|---|
| && | AND | True if both values are true | a && b |
| || | OR | False if both values are false | a || b |
| ! | NOT | Complements the value | a ! b |
Ternary Operator
This operator needs three operands and executes with some condition. It is same as if statement which we will learn later.
| Operator | Name | Description | Example |
|---|---|---|---|
| ? : | Ternary If | Condition ? True : False | a > b ? a - b : b - a |
Assignment Operator
This operator is used to assign some value to the variable. We have been using one since beginning. Remember =, yes it is also assignment operator. Take a loot at more:
| Operator | Example | Same as |
|---|---|---|
| = | x = 5 | x = 5 |
| += | x += 5 | x = x + 5 |
| -= | x -= 5 | x = x - 5 |
| *= | x *= 5 | x = x * 5 |
| /= | x /= 5 | x = x / 5 |
| %= | x %= 5 | x = x % 5 |
Take a look at this example:
This example shows how to use operators in java:
int c = a + b; and print c. They both are same.
Output:
Comments in Java Previous Next Associativity and Precedence of Operators in Java
