1 · Foundations · lesson 4 of 41

Operators & Expressions

Arithmetic, comparison, logical and bitwise — and Java's promotion rules.

Java
int  sum   = 5 + 3;      // 8
int  div   = 7 / 2;      // 3  (integer division!)
double d   = 7 / 2.0;    // 3.5
int  mod   = 10 % 3;     // 1
boolean eq = (a == b);   // reference equality for objects
boolean ok = ready && !done;
int mask   = flags & 0xFF;   // bitwise AND
WATCH OUT
== on Strings compares references, not content. Use .equals() to compare values.
Key takeaways
  • ✓Integer / integer = integer. Promote one side to get a double.
  • ✓&& and || short-circuit; & and | do not (and also work bitwise).
  • ✓For objects, == checks identity, .equals() checks value.