Operator
Operator
Operators:
Example:
int num1 = 500;
int num2 = 100;
System.out.println(num1+num2);
Types of Operators in Java:
1. Arithmetic Operators
2. Unary Operators
3. Assignment Operator
4. Relational Operators
5. Logical Operators
6. Ternary Operator
7. Bitwise Operators
8. Shift Operators
9. instance of operator
1. Arithmetic Operators
Arithmetic Operators are used to perform simple arithmetic operations on primitive and non-primitive
data types.
● * : Multiplication
● / : Division
● % : Modulo
● + : Addition
● – : Subtraction
2. Unary Operators
Unary Operators need only one operand. They are used to increment, decrement, or negate a value.
● - , Negates the value.
● + , Indicates a positive value (automatically converts byte, char, or short to int).
● ++ , Increments by 1.
○ Post-Increment: Uses value first, then increments.
○ Pre-Increment: Increments first, then uses value.
● -- , Decrements by 1.
○ Post-Decrement: Uses value first, then decrements.
○ Pre-Decrement: Decrements first, then uses value.
● ! , Inverts a boolean value.
3. Assignment Operator
‘=’ Assignment operator is used to assign a value to any variable. It has right-to-left associativity, i.e. value
given on the right-hand side of the operator is assigned to the variable on the left, and therefore right-hand
side value must be declared before using it or should be a constant.
In many cases, the assignment operator can be combined with others to create shorthand compound
statements. For example, a += 5 replaces a = a + 5. Common compound operators include:
● += , Add and assign.
● -= , Subtract and assign.
● *= , Multiply and assign.
● /= , Divide and assign.
● %= , Modulo and assign.
4.Relational Operators
Relational Operators are used to check for relations like equality, greater than, and less than. They return
boolean results after the comparison and are extensively used in looping statements as well as conditional
if-else statements. Relational operators compare values and return boolean results:
● == , Equal to.
● != , Not equal to.
● < , Less than.
● <= , Less than or equal to.
● > , Greater than.
● >= , Greater than or equal to.
5.Logical Operators
Logical Operators are used to perform “logical AND” and “logical OR” operations, similar to AND gate and
OR gate in digital electronics. They have a short-circuiting effect, meaning the second condition is not
evaluated if the first is false.
Conditional operators are:
● &&, Logical AND: returns true when both conditions are true.
● ||, Logical OR: returns true if at least one condition is true.
● !, Logical NOT: returns true when a condition is false and vice-versa
6. Ternary operator
The Ternary Operator is a shorthand version of the if-else statement. It has three operands and hence the
name Ternary.
The above statement means that if the condition evaluates to true, then execute the statements after the ‘?’
else execute the statements after the ‘:’.
7. Bitwise Operators
Bitwise Operators are used to perform the manipulation of individual bits of a number and with any of the
integer types. They are used when performing update and query operations of the Binary indexed trees.
● & (Bitwise AND) – returns bit-by-bit AND of input values.
● | (Bitwise OR) – returns bit-by-bit OR of input values.
● ^ (Bitwise XOR) – returns bit-by-bit XOR of input values.
● ~ (Bitwise Complement) – inverts all bits (one’s complement).
8. 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.
● << (Left shift) – Shifts bits left, filling 0s (multiplies by a power of two).
● >> (Signed right shift) – Shifts bits right, filling 0s (divides by a power of two), with the leftmost bit
depending on the sign.
● >>> (Unsigned right shift) – Shifts bits right, filling 0s, with the leftmost bit always 0.
9. instanceof operator
The instance of operator is used for type checking. It can be used to test if an object is an instance of a class,
a subclass, or an interface.
Advantages of Operators
The advantages of using operators in Java are mentioned below:
1. Expressiveness: Operators in Java provide a concise and readable way to perform complex calculations and
logical operations.
2. Time-Saving: Operators in Java save time by reducing the amount of code required to perform certain tasks.
3. Improved Performance: Using operators can improve performance because they are often implemented at
the hardware level, making them faster than equivalent Java code.
Disadvantages of Operators
The disadvantages of Operators in Java are mentioned below:
1. Operator Precedence: Operators in Java have a defined precedence, which can lead to unexpected results if
not used properly.
2. Type Coercion: Java performs implicit type conversions when using operators, which can lead to unexpected
results or errors if not used properly