Common Mistakes: boolean vs Numeric Types
What you'll learn: You'll discover why Java booleans are strictly logical values and cannot be treated as numbers—a common source of confusion for beginners.
The Core Idea
In some programming languages (like C or C++), the number 0 means "false" and any non-zero number means "true." Java is different. In Java, boolean is its own separate type with only two possible values: true and false. You cannot convert these to numbers, and you cannot use numbers where a boolean is expected.
What You Cannot Do
boolean flag = true;
int number = flag; // ERROR! Cannot assign boolean to int
int value = 1;
boolean result = value; // ERROR! Cannot assign int to boolean
Java will not let you cast or convert between boolean and any numeric type—not int, not long, not byte, not even char (even though char is technically numeric).
Think of It Like This
Imagine a light switch and a thermometer. A light switch has two states: ON or OFF. A thermometer shows a range of numbers. You can't ask "What temperature is ON?" or "Is 72 degrees switched on?" They're completely different kinds of information. That's exactly how Java treats boolean and numeric types—they're fundamentally incompatible.
Why This Matters
This strictness prevents subtle bugs. You must explicitly write comparison expressions that evaluate to true or false:
int count = 5;
boolean hasItems = (count > 0); // Correct: comparison creates boolean
Key Takeaway: Java booleans are strictly logical values (true or false) and cannot be cast, converted, or mixed with numeric types—you must use explicit comparisons instead.