Operators and Expressions

Learning goals

Use arithmetic, relational, and logical operators correctly; understand operator precedence, short-circuit evaluation, and the difference between == and .equals() for objects.


Arithmetic operators

int a = 10, b = 3;
System.out.println(a + b);   // 13
System.out.println(a - b);   // 7
System.out.println(a * b);   // 30
System.out.println(a / b);   // 3  (integer division)
System.out.println(a % b);   // 1  (remainder)

System.out.println(10 / 3.0);  // 3.333...

Unary: +, -, ++, --:

int x = 5;
x++;        // post-increment: use then add
++x;        // pre-increment: add then use

Compound assignment:

x += 2;   // x = x + 2
x *= 3;

Relational and equality

int a = 5, b = 10;
a < b;    // true
a >= 5;   // true
a == 5;   // true for primitives
a != b;   // true

For reference types, == compares references (same object?), not content:

String s1 = new String("hi");
String s2 = new String("hi");
s1 == s2;           // false (different objects)
s1.equals(s2);      // true (same content)

String literals are interned—literal comparison with == can mislead in interviews; prefer .equals for content.


Logical operators

boolean p = true, q = false;
p && q;   // false (AND)
p || q;   // true  (OR)
!p;       // false (NOT)

Short-circuit:

if (obj != null && obj.isValid()) { ... }
// if obj is null, isValid() is never called

Use && and || for conditions; & and | exist as bitwise operators and non-short-circuit boolean ops (rare).


Ternary operator

int max = (a > b) ? a : b;
String label = (score >= 60) ? "pass" : "fail";

Keep ternaries simple—nested ternaries harm readability.


Bitwise operators (awareness)

int flags = 0b1010;
flags & 0b0010;   // AND
flags | 0b0100;   // OR
flags ^ 0b1111;   // XOR
~flags;           // NOT
flags << 1;       // left shift
flags >> 1;       // signed right shift
flags >>> 1;      // unsigned right shift

Common in low-level flags, permissions, and performance-sensitive code—not daily business logic.


Operator precedence (top to bottom)

  1. Postfix x++, x--
  2. Unary !, -, ++x
  3. Multiplicative *, /, %
  4. Additive +, -
  5. Relational <, >, <=, >=
  6. Equality ==, !=
  7. Logical AND &&
  8. Logical OR ||
  9. Ternary ? :
  10. Assignment =, +=, …

When in doubt, use parentheses—they clarify intent for readers and compilers alike.

boolean ok = (a > 0) && (b < 100 || c == 0);

String concatenation

+ with a String concatenates:

System.out.println("Count: " + count);
System.out.println(1 + 2 + "3");   // "33"  (left-to-right: 3 + "3")
System.out.println("1" + 2 + 3);   // "123"

For many concatenations in loops, use StringBuilder (covered in the strings chapter).


Common mistakes

  1. if (x = 5) — assignment in condition; use == for comparison (compile error for boolean expected in modern Java for non-boolean).
  2. Floating equality: 0.1 + 0.2 == 0.3 is false—use tolerance or BigDecimal.
  3. Modulo sign: -5 % 3 is -2 in Java; know your domain’s expectation.
  4. Bitwise vs logical: if (a & b) evaluates both sides; usually want &&.
  5. == on Integer objects outside -128..127 — autoboxed cache makes small integers appear equal with ==; use .equals.

Practice checkpoint

  1. Evaluate: 10 / 4 + 10 % 4

    • Answer: 2 + 2 = 4
  2. What is true || expensive() if short-circuit applies?

    • Answer: true; expensive() is not called.
  3. Write a ternary that returns "even" or "odd" for int n.

    • Answer: n % 2 == 0 ? "even" : "odd"
  4. Why is "text" + null safe but null + "text" was problematic in older code?

    • Answer: String conversion calls String.valueOf(null)"null" string; both work in modern Java for concatenation.

Interview angles

  • “Short-circuit evaluation?” — Second operand skipped when result is determined (false && x, true || x).
  • “== vs equals?”== identity for references, value equality for primitives; .equals() for logical equality (override in custom types).
  • “Operator overload?” — Java does not allow user-defined operator overloading (unlike C++ or Kotlin).

Next: /java/learn/language/control-flow/