Operators
Core Fundamentals
What are Operators?
In programming, an operator is a symbol that tells the compiler or interpreter to perform a specific mathematical, relational, or logical operation and produce a final result. They are the fundamental building blocks of expressions in any language.
For example, in the expression a + b
, the +
is an operator that tells the system to add the values of variables a
and b
.
Understanding operators is crucial because their behavior can have subtle differences between languages, especially concerning type coercion, precedence, and short-circuiting.
Common Categories of Operators
1. Arithmetic Operators
These are used to perform standard mathematical calculations.
Operator | Description | Example (if x=10, y=3 ) | Result |
---|---|---|---|
+ | Addition | x + y | 13 |
- | Subtraction | x - y | 7 |
* | Multiplication | x * y | 30 |
/ | Division | x / y | 3.33 (in Python/JS), 3 (in Java/C++ for ints) |
% | Modulus (Remainder) | x % y | 1 |
** or ^ | Exponentiation | x ** y | 1000 |
++ | Increment | x++ | 11 (returns 10 then increments) |
-- | Decrement | y-- | 2 (returns 3 then decrements) |
Interview Hot Point: Division Nuances Be aware of how different languages handle integer division.
- In languages like Python 3 and JavaScript,
/
always performs float division. - In Java and C++, if both operands are integers,
/
performs integer (truncating) division. - Python has a separate operator
//
for explicit integer division.
# Python
result1 = 10 / 3 # result1 is 3.333...
result2 = 10 // 3 # result2 is 3
// Java
// Both are integers, so the result is an integer
int result = 10 / 3; // result is 3
// To get float division, one operand must be a float/double
double result2 = 10.0 / 3; // result2 is 3.333...
// JavaScript
let result = 10 / 3; // result is 3.333...
// To get integer division, you must explicitly truncate
let result2 = Math.floor(10 / 3); // result2 is 3
2. Relational (Comparison) Operators
These operators compare two values and return a boolean result (true
or false
).
Operator | Description | Example (if a=10, b=5 ) | Result |
---|---|---|---|
== | Equal to | a == b | false |
!= | Not equal to | a != b | true |
> | Greater than | a > b | true |
< | Less than | a < b | false |
>= | Greater than or equal to | a >= b | true |
<= | Less than or equal to | a <= b | false |
Interview Hot Point: ==
vs ===
in JavaScript
This is a classic interview question.
==
(Loose Equality): Compares two values for equality after converting both values to a common type (type coercion). This can lead to unexpected results.===
(Strict Equality): Compares two values for equality without performing any type conversion. If the types are different, it returnsfalse
.
Always prefer ===
in JavaScript unless you have a specific reason for type coercion.
5 == "5" // true (string "5" is coerced to number 5)
5 === "5" // false (different types: number vs string)
0 == false // true (boolean false is coerced to number 0)
0 === false // false (different types: number vs boolean)
null == undefined // true (a special case in the spec)
null === undefined// false (different types)
3. Logical Operators
These are used to combine multiple boolean expressions.
Operator | Description | Example |
---|---|---|
&& or and | AND: true if both operands are true | (5 > 3) && (10 < 20) is true |
|| or or | OR: true if at least one operand is true | (5 < 3) || (10 < 20) is true |
! or not | NOT: Inverts the boolean value | !(5 > 3) is false |
Interview Hot Point: Short-Circuiting
Logical operators in most languages are short-circuited. This is a key optimization concept.
- For
&&
(and
), if the first operand isfalse
, the second operand is never evaluated because the whole expression must befalse
. - For
||
(or
), if the first operand istrue
, the second operand is never evaluated because the whole expression must betrue
.
This is heavily used in programming to avoid unnecessary work or errors.
// Assume user is null
// Without short-circuiting, user.isLoggedIn() would throw a NullPointerException
if (user != null && user.isLoggedIn()) {
// This code is safe because the second part is never run if user is null
System.out.println("User is logged in.");
}
# A common Python idiom for default values
def get_name(user):
# If user is not None and has a 'name' attribute, it returns user.name
# Otherwise, it returns "Guest"
return user and user.name or "Guest"
4. Bitwise Operators
These operators perform operations on the individual bits of integer values. They are less common in day-to-day web development but are critical in systems programming, performance-critical code, and certain algorithms.
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT (inverts all bits) |
<< | Left Shift (multiplies by 2 for each shift) |
>> | Right Shift (divides by 2 for each shift) |
Interview Hot Point: Clever Uses
- Check if a number is even or odd:
(x & 1) == 0
is faster thanx % 2 == 0
. - Swap two numbers without a temp variable:
a ^= b; b ^= a; a ^= b;
- Flags and Permissions: Using bits in a single integer to represent multiple boolean flags.
// Using flags to represent permissions
const int READ_PERMISSION = 1 << 0; // 0001
const int WRITE_PERMISSION = 1 << 1; // 0010
const int EXECUTE_PERMISSION = 1 << 2;// 0100
int userPermissions = READ_PERMISSION | WRITE_PERMISSION; // 0011
// Check for write permission
if (userPermissions & WRITE_PERMISSION) {
// Has write permission
}
# Fast even/odd check
n = 10
if (n & 1) == 0:
print("Even")
else:
print("Odd")
Operator Precedence
Operator precedence determines the order in which operators are evaluated in a complex expression. For example, *
and /
have higher precedence than +
and -
.
10 + 5 * 2
is evaluated as 10 + (5 * 2)
, which is 20
, not (10 + 5) * 2
, which is 30
.
When in doubt, use parentheses ()
to make the order of evaluation explicit. This improves readability and prevents bugs.
(a + b) / c
is much clearer than relying on implicit precedence.