Boolean Data Type in Java
2 min read ·
The boolean data type is used to store only two possible values:
- true
- false
It is mainly used in decision making, conditions, and logical operations.
true and false Values
A boolean variable can only store either true or false. No other values are allowed.
Correct example:
Incorrect example:
Java does not allow 0 and 1 as boolean values.
Boolean in Conditions
Boolean values are mostly used inside conditions such as if statements.
Example:
The code inside the if block runs only if the condition is true.
Another example:
Here, the boolean value is determined by a comparison.
Boolean with Comparison Operators
Comparison operators are used to compare two values.
The result of every comparison operator is always a boolean value, either true or false.
Common Comparison Operators
==equal to!=not equal to>greater than<less than>=greater than or equal to<=less than or equal to
Example
Output:
false
true
false
true
false
true
Each comparison produces a boolean result based on the relationship between the two values.