In Java, operators are special symbols that perform operations on variables and values.
They help us carry out calculations, make decisions, and control the flow of programs efficiently.
In this article, you’ll learn all types of Java operators, along with their definitions, syntax, and examples — explained in simple terms.
๐ง What Are Operators in Java?
An operator is a symbol that tells the Java compiler to perform a specific operation on one, two, or three operands.
For example:
int a = 10, b = 5;
int sum = a + b;
Here:
-
+is an operator -
aandbare operands -
sumgets the value15
๐น Types of Operators in Java
Java provides several categories of operators:
-
Arithmetic Operators
-
Relational Operators
-
Logical Operators
-
Assignment Operators
-
Unary Operators
-
Bitwise Operators
-
Ternary Operator
-
Shift Operators
-
instanceof Operator
Let’s go through each of them in detail ๐
๐งฎ 1. Arithmetic Operators
Arithmetic operators perform mathematical operations such as addition, subtraction, multiplication, etc.
| Operator | Name | Description | Example |
|---|---|---|---|
+ |
Addition | Adds two values | a + b |
- |
Subtraction | Subtracts one value from another | a - b |
* |
Multiplication | Multiplies two values | a * b |
/ |
Division | Divides numerator by denominator | a / b |
% |
Modulus | Returns the remainder | a % b |
Example:
int a = 10, b = 3;
System.out.println(a + b); // 13
System.out.println(a - b); // 7
System.out.println(a * b); // 30
System.out.println(a / b); // 3
System.out.println(a % b); // 1
⚖️ 2. Relational Operators
Relational operators compare two values and return either true or false.
| Operator | Name | Description | Example |
|---|---|---|---|
== |
Equal to | Checks if two values are equal | a == b |
!= |
Not equal to | Checks if values are not equal | a != b |
> |
Greater than | True if left is greater than right | a > b |
< |
Less than | True if left is smaller than right | a < b |
>= |
Greater or equal | True if left ≥ right | a >= b |
<= |
Less or equal | True if left ≤ right | a <= b |
Example:
int a = 10, b = 5;
System.out.println(a > b); // true
System.out.println(a == b); // false
๐งฉ 3. Logical Operators
Logical operators are used to combine two or more conditions and return a boolean result.
| Operator | Name | Description | Example |
|---|---|---|---|
&& |
Logical AND | Returns true if both conditions are true | (a > b) && (a > 0) |
| || | || | Logical OR | |
! |
Logical NOT | Reverses the result | !(a > b) |
Example:
int a = 10, b = 5;
System.out.println((a > b) && (b > 0)); // true
System.out.println((a < b) || (b > 0)); // true
System.out.println(!(a == b)); // true
๐️ 4. Assignment Operators
Assignment operators assign values to variables or combine assignment with arithmetic operations.
| Operator | Name | Description | Example | Equivalent |
|---|---|---|---|---|
= |
Simple assignment | Assigns right-hand value to left-hand variable | a = 10 |
— |
+= |
Add and assign | Adds and assigns | a += 5 |
a = a + 5 |
-= |
Subtract and assign | Subtracts and assigns | a -= 5 |
a = a - 5 |
*= |
Multiply and assign | Multiplies and assigns | a *= 5 |
a = a * 5 |
/= |
Divide and assign | Divides and assigns | a /= 5 |
a = a / 5 |
%= |
Modulus and assign | Finds remainder and assigns | a %= 5 |
a = a % 5 |
Example:
int a = 10;
a += 5; // a = 15
a *= 2; // a = 30
System.out.println(a);
๐ 5. Unary Operators
Unary operators work with a single operand.
| Operator | Name | Description | Example |
|---|---|---|---|
++ |
Increment | Increases value by 1 | a++ or ++a |
-- |
Decrement | Decreases value by 1 | a-- or --a |
+ |
Unary plus | Indicates a positive value | +a |
- |
Unary minus | Negates a value | -a |
! |
Logical NOT | Reverses boolean value | !true → false |
Example:
int a = 5;
System.out.println(++a); // 6 (pre-increment)
System.out.println(a--); // 6 (post-decrement)
System.out.println(a); // 5
๐ก 6. Bitwise Operators
Bitwise operators perform operations on bits (binary representation of integers).
| Operator | Name | Description | Example |
|---|---|---|---|
& |
Bitwise AND | Returns 1 if both bits are 1 | a & b |
| | | | | Bitwise OR | Returns 1 if any bit is 1 |
^ |
Bitwise XOR | Returns 1 if bits are different | a ^ b |
~ |
Bitwise NOT | Inverts all bits | ~a |
<< |
Left shift | Shifts bits left | a << 2 |
>> |
Right shift | Shifts bits right | a >> 2 |
>>> |
Unsigned right shift | Shifts bits right and fills with 0 | a >>> 2 |
Example:
int a = 5; // Binary: 0101
int b = 7; // Binary: 0111
System.out.println(a & b); // 5 (0101)
System.out.println(a | b); // 7 (0111)
System.out.println(a ^ b); // 2 (0010)
System.out.println(~a); // -6 (bitwise NOT)
๐ 7. Ternary Operator
The ternary operator is a shorthand for if-else conditions.
Syntax:
condition ? expression1 : expression2;
Example:
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Maximum: " + max);
Output:
Maximum: 20
๐ 8. Shift Operators
Shift operators move bits to the left or right.
| Operator | Description | Example |
|---|---|---|
<< |
Left shift (multiplies by 2) | a << 1 |
>> |
Right shift (divides by 2) | a >> 1 |
>>> |
Unsigned right shift | a >>> 1 |
Example:
int a = 8; // Binary: 1000
System.out.println(a << 1); // 16
System.out.println(a >> 1); // 4
๐งฑ 9. instanceof Operator
This operator checks whether an object belongs to a specific class or subclass.
It returns true if the object is an instance of that class.
Example:
String name = "Java";
System.out.println(name instanceof String); // true
๐งญ Summary Table of Java Operators
| Category | Operators |
|---|---|
| Arithmetic | +, -, *, /, % |
| Relational | ==, !=, >, <, >=, <= |
| Logical | &&, ` |
| Assignment | =, +=, -=, *=, /=, %= |
| Unary | ++, --, +, -, ! |
| Bitwise | &, ` |
| Ternary | ?: |
| instanceof | instanceof |
✅ Conclusion
Operators are the core foundation of Java programming.
They make it possible to perform calculations, comparisons, logic, and data manipulation in a simple and powerful way.
By understanding all these operators — including arithmetic, logical (&&, ||), and bitwise (&, |, ^) — you can write efficient, logical, and optimized Java code.
Comments
Post a Comment