Java Operators Explained
⏱ Estimated reading time: 2 min
Operators in Java are special symbols used to perform operations on variables and values. They help in building expressions, performing computations, making decisions, and controlling the flow of a program. Java supports a wide variety of operators grouped into several categories.
1. Arithmetic Operators
These operators perform basic mathematical operations.
| Operator | Description |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus (remainder) |
Example:
2. Relational (Comparison) Operators
Used to compare two values. They return a boolean result (true/false).
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
Example:
3. Logical Operators
Used to combine multiple boolean expressions.
| Operator | Meaning |
|---|---|
| && | Logical AND |
| || | Logical OR |
| ! | Logical NOT |
Example:
These operators are essential in decision-making and conditional evaluations.
4. Assignment Operators
Used to assign values to variables. The basic operator is =, and Java supports many compound assignments.
| Operator | Example | Equivalent To |
|---|---|---|
| += | a += 5 | a = a + 5 |
| -= | a -= 5 | a = a - 5 |
| *= | a *= 5 | a = a * 5 |
| /= | a /= 5 | a = a / 5 |
| %= | a %= 5 | a = a % 5 |
These operators make code more concise.
5. Increment and Decrement Operators
Used to increase or decrease the value of a variable by 1.
-
++a (pre-increment)
-
a++ (post-increment)
-
--a (pre-decrement)
-
a-- (post-decrement)
Example:
6. Bitwise Operators
Operate directly on bits and are useful in low-level programming.
| Operator | Description |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| ~ | Bitwise NOT |
| << | Left shift |
| >> | Right shift |
| >>> | Unsigned right shift |
Example:
7. Ternary (Conditional) Operator
The ternary operator ?: is a shorthand for if-else.
Syntax:
Example:
8. instanceof Operator
Used to test whether an object belongs to a particular class or interface.
Example:
Conclusion
Operators are the building blocks of Java expressions and play a crucial role in performing mathematical operations, making decisions, comparing values, and controlling program logic. Understanding the different categories of operators helps programmers write efficient, readable, and well-structured Java code.
Register Now
Share this Post
← Back to Tutorials