Boolean Type In Java
In this tutorial, you will learn about Boolean type in Java with the help of examples:- boolean is a keyword used to declare boolean data type variables by putting boolean keyword before the identifier or the variable name.
A boolean data type is declared with the boolean keyword and can only take the values true or false.
Java has a primitive type, called boolean, for logical values.
It can have only one of two possible values, true or false.
This is the type returned by all relational operators, as in the case of a < b.
boolean is also the type required by the conditional expressions that govern the control statements such as if and for.
Now we will see Examples -
Here is a program that demonstrates the boolean type -
Example 1 -
boolean x=true;
boolean a =false;
System.out.println(x);
System.out.println(a);
|
Example 2 -
// web development house
// online promotion house
// Demonstrate boolean values.
import second.*;
class Main{
public static void main(String[] args) {
boolean b;
b = true;
System.out.println("b is " + b);
b = false;
System.out.println("b is " + b);
// a boolean value can control the if statement
if(b) System.out.println("This is executed.");
b = true;
if(b) System.out.println("This is not executed.");
// outcome of a relational operator is a boolean value
System.out.println("11 > 10 is " + (11 > 10));
}
}
|
Output -