Java Operators
Operators are used to perform operations on variables mostly primitive data type variables.
If you want any type of airtmetic opeartions,like addition,subtraction,multiplications you need the respective operators.
There are 5 types of groups of operators in java :
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
1. Arithmetic Operators in Java
Operator | Meaning | Example |
+ | Addition |
1
2
|
int a = 10 ;
int b = a + 10 ;
|
|
– | Subtraction |
1
2
|
int a = 10 ;
int b = a - 5 ;
|
|
* | Multiplication |
1
2
|
int a = 10 ;
int b = a * 7 ;
|
|
/ | Division |
1
2
|
int a = 50 ;
int b = a / 5 ;
|
|
% | Remainder / Modulo |
1
2
|
int a = 11 ;
int b = a % 3 ;
|
|
++ | Increment |
|
— | Decrement |
|
2. Java Assignment Operators
They are used to assign values to variables.
Examples :
Operator | Example | Same As |
|
= | x = 5 | x = 5 |
|
+= | x += 3 | x = x + 3 |
|
-= | x -= 3 | x = x - 3 |
|
*= | x *= 3 | x = x * 3 |
|
/= | x /= 3 | x = x / 3 | |
%= | x %= 3 | x = x % 3 |
|
&= | x &= 3 | x = x & 3 |
|
|= | x |= 3 | x = x | 3 | |
^= | x ^= 3 | x = x ^ 3 |
|
>>= | x >>= 3 | x = x >> 3 | |
<<= | x <<= 3 | x = x << 3 |
|
3. Logical Comparison Operators in Java
It is used to compare two values:
Operator | Meaning | Example |
< | less than |
1
2
3
4
5
|
int a = 10 , b = 20 ;
if (a < b) {
}
|
|
> | greater than |
1
2
3
4
5
|
int a = 10 , b = 20 ;
if (a > b) {
}
|
|
<= | less than or equal to |
1
2
3
4
5
|
int a = 10 ;
if (a <= 10 ) {
}
|
|
>= | greater than or equal to |
1
2
3
4
5
|
int b = 20 ;
if (b >= 20 ) {
}
|
|
== | is it equal to |
1
2
3
4
5
|
int a = 10 , b = 20 ;
if (a == b) {
}
|
|
!= | is not equal to |
1
2
3
4
5
|
int a = 10 , b = 20 ;
if (a != b) {
}
|
|
4.Logical Operators in Java
Operator | Meaning | Example |
&& | AND |
1
2
3
4
5
|
boolean a = true , b = false ;
if (a && b) {
}
|
|
|| | OR |
1
2
3
4
5
|
boolean a = true , b = false ;
if (a || b) {
}
|
|
! | NOT |
1
2
3
4
5
|
boolean a = true ;
if (!a) {
}
|
|
5. Bitwise Operators in Java
Operator | Meaning | Example |
& | bitwise AND |
|
| | bitwise OR |
|
^ | bitwise XOR |
|
~ | bitwise NOT / complement |
|
<< | left shift |
|
>> | right shift |
|
>>> | right shift with zero fill |
|
Shorthand Operators
Operator | Meaning | Example |
x += 1; | x = x + 1; |
|
x -= 1; | x = x – 1; |
|
x *= 2; | x = x * 2; |
|
x /= 2; | x = x / 2; |
|
x %= 2; | x = x % 2; |
|
Operator Precedence in Java
Operator | Meaning |
. | Member Selection |
function() | Function Call |
arr[] | Array’s Random Access |
-var | Unary Minus |
var++ | Postfix Increment |
var– | Postfix Decrement |
++var | Prefix Increment |
–var | Prefix Decrement |
! | Logical Negation |
~ | Complement |
(data_type) | Type Casting |
* | Multiplication |
/ | Division |
% | Modulo Division |
+ | Addition |
– | Subtraction |
<< | Left Shift |
>> | Right Shift |
>>> | Right Shift with Zero Fill |
< | Less than |
<= | Less than or equal to |
> | Greater than |
>= | Greater than or equal to |
instanceof | Object-Class comparison |
== | Is it equal to |
!= | Is not equal to |
& | Bitwise AND |
^ | Bitwise XOR |
| | Bitwise OR |
&& | Logical AND |
|| | Logical OR |
(condition) ? true : false | Ternary Operator |
0 Comments