Boolean Class
1. What Is Boolean? (beginner)
boolean = true/false only (no 0/1 like C!). Boolean = wrapper (nullable, storable in lists, parse-able). Conditions, flags, login checks live here!
2. Code — Flags + Parse
boolean pass = true; // primitive (default false if field!)
Boolean obj = Boolean.valueOf("TrUe"); // parse IGNORING case → true!
System.out.println(obj); // true
System.out.println(Boolean.parseBoolean("yes")); // false (only "true" → true!)
System.out.println(Boolean.logicalAnd(true, false)); // false
System.out.println(Boolean.logicalOr(true, false)); // true
// flag pattern (exam!):
boolean loggedIn = false;
// ... after password check ...
loggedIn = true;
if (loggedIn) System.out.println("welcome!");
3. Key Members
| Member | Use |
|---|---|
Boolean.parseBoolean("true") |
String → boolean (case-insensitive, only true→true!) |
Boolean.valueOf(...) |
object form |
Boolean.logicalAnd/Or/Xor(a,b) |
explicit logic (no short-circuit confusion!) |
obj.booleanValue() |
→ primitive |
4. Traps
if (5)/if (1)ERROR (needs boolean! unlike C).parseBoolean("yes")= false (NOT error! only "true"→true).Booleannull + unbox → NPE:Boolean b = null; if (b)CRASH! (useBoolean.TRUE.equals(b)safe!).
5. Exam Points
- Parse rules + flag program — 3-5 marks, frequent.
if(5)error + null-unbox NPE — 3 marks, favourite traps.