The boolean data type in Java
Java has eight primitive data types to store data in its programs. The boolean Java is one of the primitive data types.
The Boolean data type:
- A Boolean variable may have two possible values: True or False.
- The default value of the Java Boolean variable is false.
- An example of using the Boolean variable is in the conditional statement like the if, switch etc.
An example of the Boolean data type
The example below declares and uses a boolean data type variable. The Boolean variable is assigned a value and used in the if statement.
Experience this online
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class variable_example {
public static void main(String []args) {
boolean a=true;
if (a==true){
System.out.println("condition is true"); // Java Variable example
}
}
}
|
After running the above code in an editor, the output will be:
Condition is true
In the above example, you can see a boolean variable is declared and value is assigned at the time of declaration. As such, a Boolean variable may have either of two values: true or false, we assigned it true at the time of declaration.
boolean a=true;
After that, an if statement is used to check if the value of the Boolean variable is true. After that, it displayed a message on the screen by using the System.out.println statement.
Leave A Comment?