This blog is all about to providing tutorial help related to programming languages like Java, Python etc.
Friday, January 26, 2018
Sunday, May 29, 2016
Java Operators
We have five types of operators:
·
Arithmetic Operators
·
Assignment Operators
·
Logical Operators
·
Relational Operators
·
Bitwise Operators
Arithmetic Operators:
These operators are used for any type of
mathematical operation or calculations like traditional mathematics. Below is
list of them.
S No
|
Operators
|
Description
|
Example
|
1
|
+
|
Addition
|
int
a=10;
int b = 20; int sum = a+b; This will give you 30. |
2
|
-
|
Subtraction
|
int
a=10;
int b = 20; int sum = a-b; This will give you -20. |
3
|
*
|
Multiplication
|
int
a=10;
int b = 20; int sum = a*b; This will give you 200. |
4
|
/
|
Division
|
int
a=10;
int b = 20; int sum = a/b; This will give you 0.5. |
5
|
%
|
Modulus
|
int
a=20;
int b = 35; int sum = a%b; This will give you 15. |
6
|
++
|
Increment
|
int
a=10;
int increment = a++; This will give you 11. |
7
|
--
|
Decrement
|
int
a=10;
int increment = a--; This will give you 9. |
The Assignment
Operators:
Always keep in mind that these types of
operators always use with = equals to sign. Below is the list.
S NO
|
Operators
|
Description
|
Example
|
1
|
=
|
Simple assignment
operator, Assigns values from right side operands to left side operand.
|
C = A + B will
assign value of
A + B into C
|
2
|
+=
|
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
|
3
|
-=
|
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
|
4
|
*=
|
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
|
5
|
/=
|
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
|
6
|
%=
|
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
|
7
|
<<=
|
Left shift AND assignment operator.
|
C <<= 2 is same as C = C << 2
|
8
|
>>=
|
Right shift AND
assignment operator.
|
C >>= 2 is same as C = C >> 2
|
9
|
&=
|
Bitwise AND assignment operator.
|
C &= 2 is same as C = C & 2
|
10
|
^=
|
Bitwise exclusive OR and assignment operator.
|
C ^= 2 is same as C = C ^ 2
|
11
|
|=
|
Bitwise inclusive OR and assignment operator.
|
C |= 2 is same as C = C | 2
|
The Logical Operators:
The following table lists the logical operators:
Assume Boolean variables A holds true and
variable B holds false.
S No.
|
Operators
|
Description
|
Example
|
1
|
&& (logical and)
|
Called Logical
AND operator. If both the operands are non-zero, then the condition becomes
true.
|
(A &&
B) is false.
|
2
|
|| (logical or)
|
Called Logical
OR Operator. If any of the two operands are non-zero, then the condition
becomes true.
|
(A || B) is
true.
|
3
|
! (logical not)
|
Called Logical
NOT Operator. Use to reverses the logical state of its operand. If a
condition is true then Logical NOT operator will make false.
|
!(A &&
B) is true.
|
The Relational
Operators:
There are following relational operators
supported by Java language
Assume variable A holds 10 and variable B holds
20:
S NO
|
Operators
|
Description
|
Example
|
1
|
== (equal to)
|
Checks if the
values of two operands are equal or not, if yes then condition becomes true.
|
(A == B)
is not true.
|
2
|
!= (not equal to)
|
Checks if the
values of two operands are equal or not, if values are not equal then
condition becomes true.
|
(A != B) is
true.
|
3
|
> (greater than)
|
Checks if the
value of left operand is greater than the value of right operand, if yes then
condition becomes true.
|
(A > B) is
not true.
|
4
|
< (less than)
|
Checks if the
value of left operand is less than the value of right operand, if yes then
condition becomes true.
|
(A < B) is
true.
|
5
|
>= (greater than or equal to)
|
Checks if the
value of left operand is greater than or equal to the value of right operand,
if yes then condition becomes true.
|
(A >= B) is
not true.
|
6
|
<= (less than or equal to)
|
Checks if the
value of left operand is less than or equal to the value of right operand, if
yes then condition becomes true.
|
(A <= B) is
true.
|
The Bitwise Operators:
Java defines several bitwise operators, which
can be applied to the integer types, long, int, short, char, and byte.
Bitwise operator works on bits and performs
bit-by-bit operation. Assume if a = 63; and b = 13.
S No
|
Operators
|
Description
|
Example
|
1
|
& (bitwise and)
|
Binary AND Operator copies a bit to the result
if it exists in both operands.
|
(A & B) will give 12 which is 0000 1100
|
2
|
| (bitwise or)
|
Binary OR Operator copies a bit if it exists
in either operand.
|
(A | B) will give 61 which is 0011 1101
|
3
|
^ (bitwise XOR)
|
Binary XOR Operator copies the bit if it is
set in one operand but not both.
|
(A ^ B) will give 49 which is 0011 0001
|
4
|
~ (bitwise compliment)
|
Binary Ones Complement Operator is unary and
has the effect of 'flipping' bits.
|
(~A ) will give -61 which is 1100 0011 in 2's
complement form due to a signed binary number.
|
5
|
<< (left shift)
|
Binary Left Shift Operator. The left operands
value is moved left by the number of bits specified by the right operand
|
A << 2 will give 240 which is 1111 0000
|
6
|
>> (right shift)
|
Binary Right Shift Operator. The left operands
value is moved right by the number of bits specified by the right operand.
|
A >> 2 will give 15 which is 1111
|
7
|
>>> (zero fill right shift)
|
Shift right zero fill operator. The left
operands value is moved right by the number of bits specified by the right
operand and shifted values are filled up with zeros.
|
A >>>2 will give 15 which is 0000
1111
|
Subscribe to:
Posts (Atom)