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.

OperatorDescriptionExample (if x=10, y=3)Result
+Additionx + y13
-Subtractionx - y7
*Multiplicationx * y30
/Divisionx / y3.33 (in Python/JS), 3 (in Java/C++ for ints)
%Modulus (Remainder)x % y1
** or ^Exponentiationx ** y1000
++Incrementx++11 (returns 10 then increments)
--Decrementy--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

2. Relational (Comparison) Operators

These operators compare two values and return a boolean result (true or false).

OperatorDescriptionExample (if a=10, b=5)Result
==Equal toa == bfalse
!=Not equal toa != btrue
>Greater thana > btrue
<Less thana < bfalse
>=Greater than or equal toa >= btrue
<=Less than or equal toa <= bfalse

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 returns false.

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.

OperatorDescriptionExample
&& or andAND: true if both operands are true(5 > 3) && (10 < 20) is true
|| or orOR: true if at least one operand is true(5 < 3) || (10 < 20) is true
! or notNOT: 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 is false, the second operand is never evaluated because the whole expression must be false.
  • For || (or), if the first operand is true, the second operand is never evaluated because the whole expression must be true.

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.");
}

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.

OperatorDescription
&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 than x % 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
}

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.