JAVA-Operators - Notes By ShariqSP
Types of Operators in Java
Operators in Java are symbols that perform operations on operands (variables, constants, or expressions). They are used to manipulate data and perform various computations in Java programs. Java supports several types of operators, including unary, binary, and ternary operators.
Unary Operators
Unary operators act on a single operand. They are used to perform operations such as incrementing, decrementing, negating, and complementing.
- Increment (++): Increases the value of the operand by 1.
- Decrement (--): Decreases the value of the operand by 1.
- Negation (-): Negates the value of the operand.
- Logical Complement (!): Inverts the logical value of the operand.
- Bitwise Complement (~): Flips all the bits of the operand.
Binary Operators
Binary operators act on two operands. They are used to perform arithmetic, logical, bitwise, and relational operations.
Ternary Operator
The ternary operator (?:) is a conditional operator that evaluates a boolean expression and returns one of two values based on the result of the evaluation.
Java Operators
Operator Group | Operators |
---|---|
Arithmetic | +, -, *, /, % |
Logical | && (logical AND), || (logical OR), ! (logical NOT) |
Relational | ==, !=, >, <, >=, <= |
Assignment | =, +=, -=, *=, /=, %= |
Bitwise | & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise complement), << (left shift), >> (right shift), >>> (unsigned right shift) |
Ternary | ?: (conditional operator) |
Java Operators priority
Operator | Priority |
---|---|
* | 1 |
/ | 1 |
% | 1 |
+ | 2 |
- | 2 |
<< | 3 |
>> | 3 |
>>> | 3 |
> | 4 |
< | 4 |
>= | 4 |
<= | 4 |
== | 5 |
!= | 5 |
& | 6 |
^ | 7 |
| | 8 |
&& | 9 |
|| | 10 |
?: | 11 |
= | 12 |
+= | 12 |
-= | 12 |
*= | 12 |
/= | 12 |
%= | 12 |
<<= | 12 |
>>= | 12 |
>>>= | 12 |
&= | 12 |
|= | 12 |
^= | 12 |
~ | 13 |
! | 13 |