Java operators are symbols used to execute operations on variables and operate on the values of the operands. Each operator performs certain operations. Let us view an expression 5 + 1 = 6; here, 5 and 1 are operands, and the symbol + (plus) is called the operator.
A unaryoperator needs only a single operand. This operator is used to increment or decrement the value, neutralize an expression, or invert a boolean value.
++ (Increment): This operator is used to increment the value by 1. There are two types of increment, i.e., pre-increment (++a) and post-increment (a++).
— (Decrement): This operator is used to decrement the value by 1. There are two types of decrement, i.e., pre-decrement (–a) and post-decrement (a–).
! (Invert): This operator in Java is used to invert a boolean value (!a).
2.Arithmetic Operator:
Arithmetic operators are used to conducting addition, subtraction, multiplication, division, and modulus. It behaves as a mathematical operation.
+ (Addition): This operator is used for adding the values of the operands.
– (Subtraction): This operator is used for subtracting the right-hand operator from the left-hand operator.
* (Multiplication): This operator is used for multiplying the value of the operands.
/ (Division): This operator is used for dividing the left-hand operator with the right-hand operator.
Modulus: This operator is used for dividing the left-hand operator with the right-hand operator and returning the remainder.
3.Assignment Operator:
Assignment operators are used to assigning a new value to a variable. The right side operand of the assignment operator is called value, and the left side operand of the assignment operator is called variable.
=: This operator is used for assigning the value on the right to the operand on the left.
+=: This operator is used for adding the right operand to the left operand and assigning the result to the left operand.
-=: This operator is used for subtracting the right operand from the left operand and assigning the result to the left operand.
*: This operator is used for multiplying the right operand with the left operand and assigning the result to the left operand.
/=: This operator is used for dividing the left operand with the right operand and assigning the result to the left operand.
=: This operator executes the exponential calculation on operators and assigns a value to the left operand.
This operator is used to divide the left-hand operator with the right-hand operator and assign the result to the left operand.
4.Logical Operator:
Logical operators are used to connecting two or more conditions or complement the conclusion of the original condition under contemplation.
&& (logical AND): If both the operands are true, the operator returns true; otherwise, it returns false.
|| (logical OR): If either of the operands is true, the operator returns True; otherwise, it returns False.
(Logical AND): If an operand is false, the operator returns true. It reverses the logical state of an operand.
5. Shift Operator:
The shift operator is used to shift the bits of a number right or left by dividing or multiplying the numbers.
(Left Shift): This shift operator left shifts the bits of the first operand; the second operand determines the number of places to shift.
>> (Right Shift): This shift operator right shifts the bits of the first operand; the second operand determines the number of places to shift.
6.Bitwise Operator:
The bitwise operator operates on a bit string, binary number, or bit array. It is quick and simple, and the processor directly supports it. The bitwise operation is also called "bit-level programming."
(Bitwise AND): This bitwise operator takes two numbers as operands and does AND on every bit of the two numbers.
(Bitwise OR): This bitwise operator takes two numbers as operands and does OR on every bit of the two numbers.
(Bitwise XOR): This bitwise operator takes two numbers as operands and does XOR on every bit of the two numbers.
(Bitwise NOT): This bitwise operator takes one number as an operand and converts all bits of that number.
Conclusion
Overall, operators are an essential part of Java and are necessary for performing different operations on data. There are many different types of operators in Java, each with a certain objective. While some operators are used more frequently than others, all of them can be helpful in the right circumstances.