Introduction to Java · Introduction to Java Programming

Boolean Operators

Watch lectures

Boolean Operators

1. What Makes Them Different (beginner)

Boolean operators (&, |, ^) look like logical ones but always evaluate BOTH sides (no short-circuit). Plus ^ (XOR: exactly-one-true). Work on booleans; & | ^ also work on integer bits (see Bitwise topic).

2. && vs & — The Exam Trap

int y = 0;
// && skips right (y++ never runs)
if (true || y++ > 0) {}
System.out.println(y); // 0 (skipped!)
// | runs both (y++ runs!)
if (true | y++ > 0) {}
System.out.println(y); // 1 (ran!)
Op Short-circuit? Use when
&& || Yes (skip if decided) default for if (safe + fast)
& | (boolean) No (always both) need side effect / timing both
^ (XOR) No exactly one true: a ^ b

XOR truth: true ^ true = false, true ^ false = true.

3. Code — Login Check Needs Both Validations Logged

boolean validName = checkName(); // prints "checking name"
boolean validPass = checkPass(); // prints "checking pass"
// want BOTH messages even if first fails → use & (not &&)!
if (validName & validPass) System.out.println("login ok");

4. Traps

  • Default to &&/|| in if. Use single &/| only when you deliberately need both sides run.
  • ^ on booleans = XOR, on ints = bitwise XOR (different topic, same symbol!).

5. Exam Points

  • && vs & trace (y++ runs?) — 5 marks, all-time favourite.
  • XOR table — 2-3 marks, frequent.
Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.

← All Introduction to Java Programming modules