How to declare and use the Java Boolean data type

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

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.

Was this article helpful?

Related Articles

Leave A Comment?