TYPES OF OPERATORS
Operator is basically a symbol that represents an operation. Operators in java are divided into the following categories based on the type of operation they perform.
- Arithmetic Operators
- Binary: works with two operands Eg: Addition, Subtraction etc
- Unary: works with single operand Eg: Increment operator
- Assignment Operators
- Relational Operators
- Boolean Operators
- Bitwise Operators
- Class and Object Operators
- Other Operators
Arithmetic Operators
Binary Operators |
||
Operator |
Use |
Description |
+ |
A+B |
Adds A and B |
– |
A-B |
Subtracts B from A |
* |
A*B |
Multiplies A by B |
/ |
A/B |
Divides A by B |
% |
A%B |
Computes the remainder of dividing A by B |
Unary Operators |
||
Operator |
Use |
Description |
++ Post increment |
A++ |
The value is assigned before the increment is done Eg: A=1; B=A++; B will hold 1 and A will hold 2 |
++ Pre increment |
++A |
The value is assigned after the increment is done Eg: A=1; B=++A; B will hold 2 and A will hold 2 |
— Post Decrement |
A– |
Value is assigned before the decrement is made eg: A=1; B=A–; B will hold 1 and A will hold 0 |
— Pre Decrement |
–A |
Value is assigned after the decrement is made eg: A=1; B=–A; B will hold 0 and A will hold 0 |
Assignment Operators
The basic assignment operator ‘=’ is used to assign value to the variables. For example A = B; in which we assign value of B to A. Similarly, let us see how to assign values to different data types.
int Integer = 100; float Float = 10.5; char Character = ‘S’; boolean aboolean = true;
With basic assignment operator, the Java programming language defines short cut assignment operators that allow you to perform arithmetic, shift, or bitwise operation with one operator.
Now let us see, if you want to add a number to a variable and assign the result back into the same variable, so you will write something like i = i + 2; but using shortcut operator ‘+=’ You can shorten this statement, like i += 2. But remember, i = i + 2; and i += 2; both statements are the same for the compiler.
Use of Shortcut Assignment Operators
Assignment Operators |
Use |
Equivalent to |
+= |
A += B |
A=A+B |
-= |
A -= B |
A=A-B |
*= |
A *= B |
A=A*B |
/= |
A /= B |
A=A/B |
%= |
A %= B |
A=A%B |
&= |
A &= B |
A=A&B |
|= |
A |= B |
A=A|B |
^= |
A ^= B |
A=A^B |
<<= |
A <<= B |
A=A<<B |
>>= |
A >>= B |
A=A>>B |
>>>= |
A >>>= B |
A=A>>>B |
Relational Operators
Relational operators also called, comparison operators, are used to determine the relationship between two values in an expression.
Relational Operators
Operator |
Use |
Returns true if |
> |
A>B |
A is greater than B |
>= |
A>B |
A is greater than equal to B |
< |
A>B |
A is less than B |
<= |
A>B |
A is less than or eqal to B |
== |
A>B |
A and B are equal |
!= |
A>B |
A and B are not equal |
Boolean Operators
Boolean operators allow you to combine the results of multiple expressions to return a single value that evaluates to either true or false.