In Java, operators are special symbols or keywords used to perform operations on variables and values. They are essential in writing expressions and manipulating data in a program.
1️⃣. Arithmetic Operators
Arithmetic operators in Java are used to perform basic mathematical calculations(like addition, subtraction, multiplication, division, and modulus) on numeric data types like int
, float
, double
, etc.
These operators form the foundation of most computations in programming.
Operator | Description | Syntax | Example |
---|---|---|---|
+ | Addition | a + b | 5 + 3 = 8 |
– | Subtraction | a - b | 5 - 3 = 2 |
* | Multiplication | a * b | 5 * 3 = 15 |
/ | Division | a / b | 10 / 2 = 5 |
% | Modulus (Remainder) | a % b | 10 % 3 = 1 |
✅ Example:
int a = 10, b = 3;
System.out.println(a + b); // Output: 13
System.out.println(a - b); // Output: 7
System.out.println(a * b); // Output: 30
System.out.println(a / b); // Output: 3
System.out.println(a % b); // Output: 1
2️⃣. Relational (Comparison) Operators
Relational operators are used to compare two values and return a result of type boolean
— either true
or false
.
They are essential when you want to make decisions in your code, such as in conditional statements (if
, while
, etc.).
Operator | Description | Syntax | Example |
---|---|---|---|
== | Equal to | a == b | 5 == 3 → false |
!= | Not equal to | a != b | 5 != 3 → true |
> | Greater than | a > b | 5 > 3 → true |
< | Less than | a < b | 5 < 3 → false |
>= | Greater or equal | a >= b | 5 >= 3 → true |
<= | Less or equal | a <= b | 5 <= 3 → false |
✅ Example:
int a = 5, b = 10;
System.out.println(a == b); // false
System.out.println(a != b); // true
System.out.println(a < b); // true
System.out.println(a >= b); // false
3️⃣. Logical Operators
Logical operators are used to combine multiple boolean expressions or conditions and return a result of type boolean
(true
or false
).
They are especially useful when you need to make decisions based on multiple conditions in your program
Operator | Description | Syntax | Example Expression |
---|---|---|---|
&& | Logical AND | (a > b) && (b > c) | true if both conditions are true |
|| | Logical OR | `(a > b) | |
! | Logical NOT | !(a > b) | Inverts boolean value (true → false, false → true) |
✅ Example:
int a = 5, b = 10, c = 3;
System.out.println((a < b) && (b > c)); // true
System.out.println((a > b) || (b > c)); // true
System.out.println(!(a > b)); // true
4️⃣. Assignment Operators
Assignment operators are used in Java to assign values to variables. They take the value on the right-hand side and store it in the variable on the left-hand side.
The simplest assignment operator is the =
operator.
Syntax:
variable = value;
Example:
int a = 10; // assigns the value 10 to variable a
Here, 10
is assigned to a
. The expression a = 10
not only assigns the value but also returns the assigned value (10
), which can be used in other expressions.
Operator | Description | Syntax | Example |
---|---|---|---|
= | Assign | a = b | a = 5; |
+= | Add and assign | a += b | a = a + b; |
-= | Subtract and assign | a -= b | a = a - b; |
*= | Multiply and assign | a *= b | a = a * b; |
/= | Divide and assign | a /= b | a = a / b; |
%= | Modulus and assign | a %= b | a = a % b; |
✅ Example:
int a = 5;
a += 3; // a = a + 3 → a becomes 8
System.out.println(a); // Output: 8
5️⃣. Unary Operators
Unary operators are operators that operate on a single operand to produce a new value or change the state of a variable. They are typically used for incrementing, decrementing, negating, or inverting values.
Syntax:
operator operand;
or
operand operator;
Operator | Description | Syntax | Example |
---|---|---|---|
+ | Unary plus | +a | +5 → 5 |
– | Unary minus | -a | -5 → -5 |
++ | Increment by 1 | a++ / ++a | Increment a |
— | Decrement by 1 | a-- / --a | Decrement a |
! | Logical NOT | !true | false |
✅ Example:
int a = 5;
System.out.println(++a); // Output: 6 (pre-increment)
System.out.println(a--); // Output: 6 (post-decrement; a becomes 5 after)
6️⃣. Ternary Operator
6.1. Ternary Operator
The ternary operator is a shortcut for the if-else
statement.
It’s called “ternary” because it takes three operands.
Syntax:
variable = (condition) ? valueIfTrue : valueIfFalse;
- condition → a boolean expression that evaluates to
true
orfalse
. - valueIfTrue → value assigned if the condition is
true
. - valueIfFalse → value assigned if the condition is
false
.
It returns a value, unlike if-else
, which is a statement.
6.2. How It Works
- Evaluate the condition.
- If the condition is
true
, the first value (valueIfTrue
) is used. - If the condition is
false
, the second value (valueIfFalse
) is used.
✅ Example:
int a = 10;
int b = 20;
int max = (a > b) ? a : b; // check which is larger
System.out.println("Maximum: " + max);
Output:
Maximum: 20
Explanation:
(a > b)
→10 > 20
→false
- So
valueIfFalse
(b
) is assigned tomax
.
6.3. Ternary Operator vs if-else
Using if-else
:
int a = 10;
int b = 20;
int max;
if(a > b) {
max = a;
} else {
max = b;
}
System.out.println("Maximum: " + max);
Using Ternary Operator (shorter and cleaner):
int max = (a > b) ? a : b;
The ternary operator is concise, making it useful for simple conditional assignments.
6.4. Nested Ternary Operator
You can nest ternary operators to check multiple conditions, but be careful—it can reduce readability.
Example:
int a = 5, b = 10, c = 7;
int max = (a > b) ? a : (b > c ? b : c);
System.out.println("Maximum: " + max);
Output:
Maximum: 10
Explanation:
(a > b)
→5 > 10
→false
→ evaluate(b > c ? b : c)
(b > c)
→10 > 7
→true
→b
is chosen
6.5. Key Points
- Ternary operator always returns a value.
- Can be used in assignments, print statements, or return statements.
- For multiple conditions, nest carefully—too much nesting reduces readability.
- Works with any data type: int, double, boolean, String, etc.
7️⃣. Bitwise Operators
7.1. Bitwise Operators
Bitwise operators operate on binary representations of integers at the bit level.
They are used to manipulate individual bits of numbers, which is very useful in low-level programming, flags, and performance optimization.
Applicable data types: int
, long
, short
, byte
, char
.
7.2. Types of Bitwise Operators
A. Bitwise AND (&
)
- Performs AND operation on each pair of corresponding bits.
- Result is
1
if both bits are 1, else0
.
Example:
int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
int c = a & b;
System.out.println(c); // 1 (0001 in binary)
Explanation:
Bit of a | 0 | 1 | 0 | 1 |
---|---|---|---|---|
Bit of b | 0 | 0 | 1 | 1 |
a & b | 0 | 0 | 0 | 1 → 1 |
B. Bitwise OR (|
)
- Performs OR operation on each pair of bits.
- Result is
1
if at least one bit is 1, else0
.
int c = a | b;
System.out.println(c); // 7 (0111 in binary)
C. Bitwise XOR (^
)
- Performs XOR operation on each pair of bits.
- Result is
1
if bits are different, else0
.
int c = a ^ b;
System.out.println(c); // 6 (0110 in binary)
D. Bitwise Complement (~
)
- Inverts all bits (1 → 0, 0 → 1).
- For signed integers, result =
-(n+1)
because of two’s complement.
int a = 5; // 0000 0101
int c = ~a; // 1111 1010 → -6
System.out.println(c); // -6
E. Left Shift (<<
)
- Shifts all bits to the left by a certain number of positions.
- Vacant bits are filled with 0.
- Equivalent to multiplying by
2^n
.
int a = 5; // 0101
int c = a << 2; // 0101 << 2 → 10100 (20 in decimal)
System.out.println(c); // 20
F. Right Shift (>>
)
- Shifts all bits to the right.
- For positive numbers, fills leftmost bits with
0
. - For negative numbers, fills leftmost bits with
1
(sign extension). - Equivalent to dividing by
2^n
.
int a = 20; // 10100
int c = a >> 2; // 10100 >> 2 → 0101 (5)
System.out.println(c); // 5
G. Unsigned Right Shift (>>>
)
- Shifts bits to the right without considering sign.
- Fills leftmost bits with
0
. - Only differs from
>>
for negative numbers.
int a = -20;
int c = a >>> 2;
System.out.println(c); // large positive number
Operator | Description | Syntax | Example |
---|---|---|---|
& | Bitwise AND | a & b | 5 & 3 → 1 |
| | Bitwise OR | a | b | `a |
^ | Bitwise XOR | a ^ b | 5 ^ 3 → 6 |
~ | Bitwise Complement | ~a | ~5 → -6 |
<< | Left shift | a << 2 | 5 << 2 → 20 |
>> | Right shift | a >> 2 | 5 >> 2 → 1 |
✅ Example:
int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
System.out.println(a & b); // Output: 1 (0001)
System.out.println(a | b); // Output: 7 (0111)
🎯Summary
Java provides a comprehensive set of operators that simplify performing calculations, comparisons, logical decisions, and bitwise manipulations.
Understanding their syntax and proper usage is essential for writing clean, efficient, and maintainable Java code.