Boolean Data Type
Boolean data types can acquire two values either true or false. We use these data types when we need to track the true/ false condition.
The condition are denoted by these two key words- true(for true condition) & false(for false condition).
Control Statement and Boolean
The control statements like if, for, while and do all requires boolean value to execute. Generally operators are used to evaluate a condition, which return boolean value. According to this boolean value, the control statements executes.
The given below example will give you a clear idea :
package com.roseindia; public class BooleanExample { public static void main(String[] arg) { boolean bool = true; System.out.println("Value of bool : " + bool); if (bool == true) { System.out.println("Value of boolean is true."); } else { System.out.println("Value of boolean is false."); } } }
Comparison Operator
The comparison operator is used to compare the two primitive values. The given below the operator, their name and their output :
Operator | Name | Output |
i < j | less than | true/false |
i<=j | less than or equal | true/false |
i = = j | equal | true/false |
i >= j | greater than equal | true/false |
i > j | greater than | true/false |
i != j | not equal | true/false |
Logical Operators
Given below the logical operator and their meaning :
Operator | Name | Meaning |
a && b | and | If both a & b are true, the result is true. |
a || b | or | If either a or b are true, the result is true |
!a | not | true when 'a' is false, and false when 'a' is true. |
Methods which return Boolean values
In java, there are many methods which returns Boolean value. For example, equals, instanceof operators, the methods whose name starts with "is" etc.