Boolean (Part 9)
Java Boolean
Very often, in programming, you will need a data type that can only have one of two values, like:
- YES / NO
- ON / OFF
- TRUE / FALSE
For this, Java has a boolean
data type, which can take the values true
or false
.
A boolean type is declared with the boolean
keyword and can only take the values true
or false
:
Eg:-
public class MyClass {
public static void main(String[] args) {
boolean b1=true;
boolean b2=false;
boolean b3=(b1==b2);
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
}
}
Output:-
true
false
false