Operators in Java are special symbols or characters used to perform operations on variables or values.
Java supports a variety of operators, which can be categorized into several groups based on their functionality.
Here's an overview of the different types of operators in Java:
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, division, and modulus.
+ (addition): The `+` operator is used to add two values together.
- (subtraction): The `-` operator is used to subtract one value from another.
* (multiplication): The `*` operator is used to multiply two values.
/ (division): The `/` operator divides one value by another.
% (modulus): The `%` operator returns the remainder of the division of one value by another.
Assignment operators are used to assign values to variables.
= (simple assignment): The simple assignment operator `=` assigns the value on the right-hand side to the variable on the left-hand side.
+= (addition assignment): The addition assignment operator `+=` adds the value on the right-hand side to the variable on the left-hand side.
-= (subtraction assignment): The subtraction assignment operator `-=` subtracts the value on the right-hand side to the variable on the left-hand side.
*= (multiplication assignment): The multiplication assignment operator `*=` multiples the value on the right-hand side to the variable on the left-hand side.
/= (division assignment): The division assignment operator `/=` divides the value on the right-hand side to the variable on the left-hand side.
%= (modulus assignment): The modulus assignment operator `%=` performs the modulus operation on the variable on the left-hand side with the value on the right-hand side.
Unary operators operate on a single operand and are used to perform various operations such as incrementing, decrementing, negating, and complementing.
++ (increment): The increment `++` operators are used to increase the value of a variable by 1.
-- (decrement): The decrement `--` operators are used to decrease the value of a variable by 1.
+ (unary plus): The unary plus operator `+` simply indicates a positive value. It doesn't change the sign of the operand. for ex: int x = +2;
- (unary minus): The unary minus operator `-` negates the value of its operand, making a positive number negative and vice versa. for ex: int x = -2;
! (logical complement): The logical complement operator `!` is used to invert the value of a boolean expression.
Relational operators are used to compare two values and determine the relationship between them. They return a boolean result (true or false).
== (equal to)
!= (not equal to)
< (less than)
> (greater than)
<= (less than or equal to)
>= (greater than or equal to)
Logical operators are used to perform logical operations such as AND, OR, and NOT on boolean values.
&& (logical AND): The logical AND operator `&&` returns `true` if both of its operands are `true`, otherwise, it returns `false`.
|| (logical OR): The logical OR operator `||` returns `true` if at least one of its operands is `true`, otherwise, it returns `false`.
! (logical NOT): The logical NOT operator `!` is a unary operator that negates the value of its operand. It returns `true` if the operand is `false`, and false if an operand is `true`.
The conditional operator (? :) is a ternary operator that evaluates a boolean expression and returns one of two values depending on whether the expression is true or false.
(condition) ? value1 : value2;
Bitwise operators perform operations at the bit level on integer types.
& (bitwise AND)
| (bitwise OR)
^ (bitwise XOR)
~ (bitwise complement)
<< (left shift)
>> (right shift)
>>> (unsigned right shift)
The "instanceof" operator is used to test whether an object is an instance of a particular class or interface.
if (obj instanceof YourClass) { ... }