JAVA Theory
Operators:
There are many types operators in java. Some of the types are
Types of operators in JAVA:
- Arithmetic Operators
- Logical Operators
- Relational (Comparision) Operators
- Assignment Operators
- Bitwise Operators
Arithmetic Operators:
Arithmetic operators are used to perform an arithmetic operations like addition, subtraction, multiplication etc.
Operator | Description | Example |
---|---|---|
+ | Addition | 3+2 |
- | Subtraction | 3-2 |
* | Multiplication | 3*2 |
/ | Division | 10/5 |
% | Modulus | 10%5 |
++ | Increment and then return value | x=3; ++X |
-- | Decrement and then return value | x=3; --X |
Logical Operators:
Logical operators are used to perform logical operations like AND, OR, NOT operations.
Operator | Description | Example |
---|---|---|
&& | Logical AND evaluates to true when both operands are true | 3>2 && 5>3 |
|| | Logical OR evaluates to true when both operands is true | 3>1 || 2>5 |
! | Logical NOT evaluates to true if the operands is flase | a-=1(a=a-1) |
Relational Operators:
Relational operators are used to perform comparison operations like greater than or equal , less than or equal, equal or not etc.
Operator | Description | Example |
---|---|---|
== | Equal to | (A && B) is false |
> | Greater than | 5 > 3 is evaluated to 1 |
< | Less than | 5 < 3 is evaluated to 0 |
!= | Not equal to | 5 != 3 is evaluated to 1 |
>= | Greater than or equal to | 5 >= 3 is evaluated to 1 |
<= | Less than or equal to | 5 <= 3 is evaluated to 0 |
Assignment Operators:
Assignment operators are used to assign new value to variable. The following operations will perform.
Operator | Description | Example |
---|---|---|
== | simple Assignment operator, Assigns values from right side operands to left side operand | C = A + B Will assign value of A+B into C |
+= | Add AND assignment operator, it adds right operand to the left operand and assign the result to left operand | C += A is equivalent to C = C + A |
-= | Subtract AND assignment operator, it subtracts right operand from the left operand and assign the result to left operand | C -= A is equivalent to C = C - A |
*= | Multiply AND assignment operator, it Multiplies right operand with the left operand and assign the result to left operand | C *= A is equivalent to C = C * A |
/= | Divide AND assignment operator, it divides left operand with the right operand and assign the result to left operand | C /= A is equivalent to C = C / A |
%= | Modulus AND assignment operator, it takes modulus using two operands and assign the result to left operand | C %= A is equivalent to C = C % A |
<<= | Left Shift AND assignment operator | C <<= 2 is same as C = C << 2 |
>>= | Left Shift AND assignment operator | C >>= 2 is same as C = C >> 2 |
&= | Bitwise AND assignment operator | C &= 2 is same as C = C & 2 |
^= | Bitwise Exclusive OR and assignment operator | C ^= 2 is same as C = C ^ 2 |
|= | Bitwise inclusive OR and assignment operator | C |= 2 is same as C = C | 2 |
Bitwise Operators:
Bitwise operators are used to perform bit-level operations.
Operator | Description | Example |
---|---|---|
~ | Bitwise unary NOT | ~a |
& | Bitwise AND | a & b |
| | Bitwise OR | a | b |
^ | Bitwise EX - OR | a ^ b |
>> | Shift right | a>>1 |
>>> | Shift right Zero fill | a>>>1 |
<< | Shift left | a<<1 |
&= | Bitwise AND Assignment | a &= b |
|= | Bitwise OR Assignment | a |= b |
^= | Bitwise EX-OR Assignment | a ^= b |
>>= | Shift left assignment | a ^= b |
>>>= | Shift right Zero fill assignment | a >>>= 1 |
<<= | Shift left assignment | a <<= 1 |