RL

Java Operators & Expressions

Operators

Definition: Symbols that do stuff to values — compute, compare, or test conditions. Think: +, -, *, ==, &&, etc. Operators take operands (yung values) and return a result.


Arithmetic Operators

What they do: Basic math.

  • + (add), - (subtract), * (multiply), / (divide), % (modulus — remainder).
    Example: x = y + z → adds y and z.
    Tip: For numeric + it adds; for Strings, + concatenates (joins) — e.g., "hi" + "you""hiyou". If one operand is String, Java converts the other to String first.


Assignment Operators

What they do: Assign value(s) to variables.

  • = basic assignment (x = y)

  • += (add and assign), -= (subtract and assign), *= , /= , %=
    Example: x += 3; is same as x = x + 3;
    Tip: You can chain: x = y = 0; sets both to 0.


Unary Operators

What they do: Need only one operand.

  • + unary plus (positive), - unary minus (negative)

  • ++ increment (add 1), -- decrement (minus 1)

  • ! logical NOT (invert boolean)
    Pre vs Post:

int x = 7;
int a = ++x; // pre-increment: x becomes 8, a = 8
int y = 7;
int b = y++; // post-increment: b = 7, then y becomes 8

Tip: Pre changes value first, post changes after using value.


Comparison / Relational Operators

What they do: Compare values → return true/false.

  • == equal, != not equal, > greater, < less, >=, <=
    Example: x == y checks equality.


Shift Operators (bit-level)

What they do: Move bits left/right. Works only on integer types.

  • << left shift (multiply by 2^n roughly)

  • >> right shift (divide by 2^n, keeps sign)

  • >>> unsigned right shift (fills left with zeros)
    Example: 9 >> 22 (9 / 4) ; 9 << 236 (9 * 4)
    Tip: >>> matters for negative numbers — it fills with 0 bits instead of preserving sign.


Bitwise Operators (operate bit-by-bit)

What they do: Work on individual bits of integers (and can be used on char, short, int, long, byte).

  • & AND — bit = 1 only if both bits 1

  • | OR — bit = 1 if any bit 1

  • ^ XOR — bit = 1 if bits differ

  • ~ NOT — flips bits
    Example: 5 & 31 because 0101 & 0011 = 0001.
    Also: When applied to boolean expressions, &/| act as logical operators but without short-circuiting (they evaluate both sides).


Logical Operators (for boolean logic)

What they do: Combine boolean expressions.

  • && Logical AND (short-circuit) — stops if left is false

  • || Logical OR (short-circuit) — stops if left is true
    Example: (x > 4) && (y < 8) → true only if both conditions true.
    Tip: Use &&/|| normally; &/| do similar but evaluate both sides always.


Conditional (Ternary) Operator

Syntax: (condition) ? valIfTrue : valIfFalse
What it does: Short if-else inline.
Example: x = (y > z) ? y : z; → x gets y if y>z, else z.


Order of Precedence (short version)

Meaning: Some operators run before others. Higher precedence runs first. Key groups (high → low):

  1. [], ()

  2. Unary + - ~ ! ++ --

  3. * / %

  4. + -

  5. Shift << >> >>>

  6. Relational < <= > >=

  7. Equality == !=

  8. Bitwise & ^ |

  9. Logical && ||

  10. Ternary ?:

  11. Assignment =, +=, -=, *=, /=, %=
    Tip: Use parentheses () to make intent clear and avoid mistakes.


Expressions

Definition: Any combination of variables, literals, and operators that produces a single value.
Common types:

  • Arithmetic expression: x + y returns numeric value.

  • Assignment expression: a = a + 4 modifies a.

  • Relational expression: x < y returns boolean.

  • Logical expression: (j < k) && (k < 4) returns boolean.

  • Conditional expression: using ?: to pick a value based on condition.


Quick Examples (practical)

  • Concatenate vs add:

    System.out.println(7 + 3);      // prints 10 (numeric add)
    System.out.println(2 + "abc");  // prints 2abc (string concat)
    
  • Compound assign:

    int x = 5;
    x *= 3; // x = 15
    
  • Short-circuiting:

    boolean res = (x != 0) && (10 / x > 2); // safe: second runs only if x != 0
    

Quick Tips para sa exam/focus

  • Remember difference between & (bitwise/always-evaluate) and && (logical short-circuit).

  • Pre vs post increment — know when value changes (before use or after).

  • String + number → string concat (watch for unexpected results).

  • Use parentheses to avoid precedence surprises.

  • Ternary operator is shorthand for simple if-else that returns values.