Java Operator
Operators are used to perform operations on variables and values.
Java divides the operators into the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
Arithmetic operators
It is used to perform common mathematical operations.
Arithmetic operators contains following operators;
Plus (+)
Minus(-)
Multiply (*)
Divide (/)
Modulus (%)
Increment (++)
Decrement (--)
Assignment operators
Used to assign values to variables.
Arithmetic operators contains following operators;
= += -= *= /= %= &= |= ^= >>= <<=
Example:
Assign with pre-variable +=
x += 5 means x = x+5
Comparison operators
Used to compare two values and important in programming, because it helps us to find answers and make decisions.
It returns value of a comparison is either true or false. These values are known as Boolean values.
Comparison operators contains following operators;
Equal ==
Not-equal !=
Greater than >
Lower than <
Greater equal >=
Lower equal <=
Logical operators
It can also test for true or false values with logical operators.
Logical operators are used to determine the logic between variables or values;
&& : Logical and that returns true if both statements are true
|| : Logical or that returns true if any one statements are true
! : Logical not that returns false if result true
Bitwise operators
It is used to performing the manipulation of individual bits of a number.
They can be used with any integral type (char, short, int, etc.).
They are used when performing update and query operations of the Binary indexed trees.
There are the following types of bitwise operator;
| OR: Any one can be true
& AND: All needs be satisfies
^ XOR: Different bit 1 and similar bit 0
~ Complement: Makes 0 to 1 and 1 to 0 (invert)
<< Signed Left shift (<<< for unsinged)
>> Signed Right shift (>>> for unsigned)
Bitwise: OR ( | )
It returns bit by bit OR of input values,
i.e., if either of the bits is 1, it gives 1, else it shows 0.
Example:
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
Bitwise OR Operation of 5 and 7
0101
| 0111
________
0111 = 7 (In decimal)
Bitwise: AND (&)
It returns bit by bit AND of input values,
i.e., if both bits are 1, it gives 1, else it shows 0.
Example:
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
Bitwise AND Operation of 5 and 7
0101
& 0111
________
0101 = 5 (In decimal)
Bitwise: XOR (^)
It returns bit by bit XOR of input values,
i.e., if corresponding bits are different, it gives 1, else it shows 0.
Example:
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
Bitwise XOR Operation of 5 and 7
0101
^ 0111
________
0010 = 2 (In decimal)
Bitwise: Complement (~)
It returns the one’s complement representation of the input value,
i.e., with all bits inverted, which means it makes every 0 to 1, and every 1 to 0.
Example:
a = 5 = 0101 (In Binary)
Bitwise Complement Operation of 5
~ 0101
________
1010 = 10 (In decimal)
Note: Compiler will give 2’s complement of that number, i.e., 2’s complement of 10 will be -6.
Bit-Shift Operators: (Shift Operators)
Shift operators are used to shift the bits of a number left or right, thereby multiplying or dividing the number by two, respectively.
They can be used when we have to multiply or divide a number by two.
Syntax:
number shift_op number_of_places_to_shift;
Shift Operators Types
Shift Operators are further divided into 4 types. These are:
Signed Right shift operator (>>)
Unsigned Right shift operator (>>>)
Left shift operator(<<)
Unsigned Left shift operator (<<<)