Java provides a wide range of operators that can be used to perform various operations on variables, values, and expressions. Here are the different types of operators commonly used in Java:
- Arithmetic Operators: Arithmetic operators are used to perform basic mathematical operations.
- Addition:
+
- Subtraction:
-
- Multiplication:
*
- Division:
/
- Modulus (remainder):
%
- Increment:
++
- Decrement:
--
- Addition:
- Assignment Operators: Assignment operators are used to assign values to variables.
- Simple assignment:
=
- Addition assignment:
+=
- Subtraction assignment:
-=
- Multiplication assignment:
*=
- Division assignment:
/=
- Modulus assignment:
%=
- Simple assignment:
- Comparison Operators: Comparison operators are used to compare two values and evaluate to a boolean result (
true
orfalse
).- Equal to:
==
- Not equal to:
!=
- Greater than:
>
- Less than:
<
- Greater than or equal to:
>=
- Less than or equal to:
<=
- Equal to:
- Logical Operators: Logical operators are used to combine boolean expressions and evaluate to a boolean result.
- Logical AND:
&&
- Logical OR:
||
- Logical NOT:
!
- Logical AND:
- Bitwise Operators: Bitwise operators are used to perform operations at the bit level.
- Bitwise AND:
&
- Bitwise OR:
|
- Bitwise XOR:
^
- Bitwise NOT:
~
- Left shift:
<<
- Right shift:
>>
- Unsigned right shift:
>>>
- Bitwise AND:
- Conditional (Ternary) Operator: The conditional operator (
?:
) is a shorthand way of writing anif-else
statement.- Conditional operator:
condition ? expression1 : expression2
- Conditional operator:
- Instanceof Operator: The
instanceof
operator is used to check if an object belongs to a particular class or implements an interface.instanceof
operator:object instanceof ClassName
- Other Operators: Java also provides other operators for specific purposes, such as:
- Access operators:
.
(dot) and::
(method reference operator) - Type cast operator:
(type)
- Array access operator:
[]
- Member selection operator:
.
(dot) - Method call operator:
()
(parentheses)
- Access operators:
These operators allow you to perform a wide range of operations in Java, making it a versatile programming language.