
Java Operators
Java Operators
Operators are symbols which are used to perform certain mathematical operations on the given operands (on the value of variables).
There are basically 3 types of operators in java :
- Unary Operator
- Binary Operator
- Ternary Operators
Unary Operator
When the operation of operator needs to be applied on single operand, is called Unary Operator.
Operator | Name | Description |
++ | Increment Operator | Increments the value of variable by 1. |
— | Decrement Operator | Decrements the value of variable by 1. |
! | Not Operator | Inverts the result of given expression. |
Example | Description |
x=a++; | Stores the value of a in x then increments a by 1. |
x=a–; | Stores the value of a in x then decrements a by 1. |
x=++a; | Increments the value of a by 1 then stores the new value in x. |
x=–a; | Decrements the value of a by 1 then stores the new value in x. |
boolean b; b=!(true); |
Now the value in b will be ‘false’. |
Binary Operator:
When the operation of operator needs to be applied on two operands, is called Binary Operator.
The following are 5 categories of Binary Operators.
- Arithmetic Operators.
- Assignment Operators.
- Comparison Operators.
- Logical Operators.
- Bitwise Operators.
Arithmetic Operators :
Arithmetic operators are used to perform simple mathematical calculations with the values of variables.
Here, x=5; y=2;
Operator | Name | Description | Example | Output |
+ | Addition | Adds the values of given two variables | x + y | 7 |
– | Subtraction | Subtracts one value from another | x – y | 3 |
* | Multiplication | Multiplies two values | x * y | 10 |
/ | Division | Divides one value from another | x / y | 2 |
% | Modulus | Returns the remainder of division | x % y | 1 |
Assignment Operators :
Assignment operators are used to assign value which is on the right side of the operator to the variable which is on the left side of the operator.
Here, v=10;
Operator | Example | Meaning | Output |
= | v = 10 | v = 10 | 10 |
+= | v += 4 | v = v + 4 | 14 |
-= | v -= 7 | v = v – 7 | 3 |
*= | v *= 4 | v = v * 4 | 40 |
/= | v /= 5 | v = v / 5 | 2 |
%= | v %= 6 | v = v % 6 | 4 |
&= | v &= 2 | v = v & 2 (Bitwise AND) | 2 |
|= | v |= 3 | v = v | 3 (Bitwise Inclusive OR) | 11 |
^= | v ^= 3 | v = v ^ 3 (Bitwise Exclusive OR) | 9 |
>>= | v >>= 3 | v = v >> 3 (Right Shift) | 1 |
<<= | v <<= 3 | v = v << 3 (Left Shift) | 80 |
Comparison Operators :
These operators are used to compare between two values.
Here, x=5; y=2;
Operator | Name | Example | Output |
== | Equal to | x == y | false |
!= | Not equal | x != y | true |
> | Greater than | x > y | true |
< | Less than | x < y | false |
>= | Greater than or equal to | x >= y | true |
<= | Less than or equal to | x <= y | false |
Logical Operators :
These operators are used to apply logical operations on the values of variables.
Operator | Name | Description | Example | Output |
&& | Logical AND | Returns true only if both the expressions are true. | x>3&&x<10 | true |
|| | Logical OR | Returns true even if anyone or both the expressions are true. | x>3||x<4 | true |
! | Logical NOT | Reverses the result of expression. i.e, returns false if the result is true and vice-versa. | !(x>3&&x<10) | false |
Ternary Operators(? 🙂 :
When the operation of operator needs to be applied on three operands, is called Ternary Operator. It works like an if-else statement. This operator is applied to a binary condition, based on which the result is executed.
Syntax:
answer = BinaryCondition ? Statement_If_True : Statement_If_False ; int a=10; int b = 5; String ans = (a>b)? “a is greater” : “b is greater”; System.out.println (“The answer = “+ans); |
Output:
The answer = a is greater |