java operators - CodingAliens

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

OperatorMeaningExample
+Addition
1
2
int a = 10;
int b = a + 10; // 20
Subtraction
1
2
int a = 10;
int b = a - 5; // 5
*Multiplication
1
2
int a = 10;
int b = a * 7; // 70
/Division
1
2
int a = 50;
int b = a / 5; // 10
%Remainder / Modulo
1
2
int a = 11;
int b = a % 3; // 2
++Increment
1
2
3
int a = 10;
++a; // 11
a++; // 12
Decrement
1
2
3
int a = 10;
a--; // 9
--a; // 8

2. Java Assignment Operators

They are used to assign values to variables.
Examples :
OperatorExampleSame As
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
&=x &= 3x = x & 3
|=x |= 3x = x | 3
^=x ^= 3x = x ^ 3
>>=x >>= 3x = x >> 3
<<=x <<= 3x = x << 3

3. Logical Comparison Operators in Java

It is used to compare two values:

OperatorMeaningExample
<less than
1
2
3
4
5
int a = 10, b = 20;
if (a < b) {
    // true
}
>greater than
1
2
3
4
5
int a = 10, b = 20;
if (a > b) {
    // false
}
<=less than or equal to
1
2
3
4
5
int a = 10;
if (a <= 10) {
    // true
}
>=greater than or equal to
1
2
3
4
5
int b = 20;
if (b >= 20) {
    // true
}
==is it equal to
1
2
3
4
5
int a = 10, b = 20;
if (a == b) {
    // false
}
!=is not equal to
1
2
3
4
5
int a = 10, b = 20;
if (a != b) {
    // true
}

4.Logical Operators in Java

OperatorMeaningExample
&&AND
1
2
3
4
5
boolean a = true, b = false;
if (a && b) {
    // false
}
||OR
1
2
3
4
5
boolean a = true, b = false;
if (a || b) {
    // true
}
!NOT
1
2
3
4
5
boolean a = true;
if (!a) {
    // false
}

5. Bitwise Operators in Java

OperatorMeaningExample
&bitwise AND
1
2
3
int a = 2;
a = a & 1; // 0
|bitwise OR
1
2
3
int a = 2;
a = a | 1; // 3
^bitwise XOR
1
2
3
int a = 3;
a = a ^ 1; // 2
~bitwise NOT / complement
1
2
3
4
int  a = 8;
a = ~a; // -9
// 2's complement
<<left shift
1
2
3
int a = 8;
a = a << 1; // 16
>>right shift
1
2
3
int a = 8;
a = a >> 1; // 4
>>>right shift with zero fill
1
2
3
int a = 8;
a = a >>> 1; // 4

Shorthand Operators

OperatorMeaningExample
x += 1;x = x + 1;
1
2
3
int a = 2;
a += 1; // 3
x -= 1;x = x – 1;
1
2
3
int a = 8;
a -= 1; // 7
x *= 2;x = x * 2;
1
2
3
int a = 8;
a *= 2; // 16
x /= 2;x = x / 2;
1
2
3
int a = 8;
a /= 2; // 4
x %= 2;x = x % 2;
1
2
3
int a = 8;
a %= 2; // 0

Operator Precedence in Java

OperatorMeaning
.Member Selection
function()Function Call
arr[]Array’s Random Access
-varUnary Minus
var++Postfix Increment
var–Postfix Decrement
++varPrefix Increment
–varPrefix 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
instanceofObject-Class comparison
==Is it equal to
!=Is not equal to
&Bitwise AND
^Bitwise XOR
|Bitwise OR
&&Logical AND
||Logical OR
(condition) ? true : falseTernary Operator

Post a Comment

0 Comments