Java boolean data type with example

The boolean data type

Java has eight types of primitive data types to store data in the Java 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 and is 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 Java 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 then it will print a message in the screen by using the System.out.println statement.

 

Also see – Java Data Types

Was this article helpful?

Related Articles

Leave A Comment?