Java Boolean Keyword
Boolean keyword
In java programming language we basically have two types of primitive data types, Boolean and Numeric (integer and floating-point data types). In this article we are going to learn about the Boolean keyword which is a primitive data type in java language.
Syntax:
Boolean variable_name = true/false;
- Generally, Boolean keyword plays a role in executing only two possible output values that are either true or false.
- Boolean keyword uses only 1-bit of data or information.
- So, its size cannot be mentioned exactly or precisely.
- The Boolean keyword has its default value as false.
- Boolean keyword is utilized with both the variables and methods.
- It is mainly or essentially used with conditional statements.
- In a few programs we often need data of only one or two values like
Yes/no
On /Off
True/False
- Boolean Keyword is declared or implemented using Boolean key and takes the values of true or false.
- With the Boolean keyword we can also use the comparison operators such as the greater than symbol (>), lesser than (<), equals (==) etc in checking the conditional statements.
Example
In this example we use greater than operator (>)
Int a = 10;
Int b = 9;
System.out.println(a > b); // returns true, as 10 is higher than 9
In this example we use lesser than operator (<)
int a = 10;
int b= 9;
System.out.println(a < b); //returns false due to the lesser value of the b than a
In this example we use equal to operator (==)
Int c == 20;
System.out.println(c==20) ;// returns true, as the value of the c is 20
Or
Int d==20;
System.out.println(d==13); //returns false, because value of c is 20
Values of Boolean keyword in Java
A Boolean keyword is declared by help of the Boolean type and can only achieve the values of true or false:
Example:
Boolean a java game= true;
Boolean tastes bitter= false;
System.out.println(a java game); // the value of outputs true
System.out.println(tastes bitter); // the value of outputs false
However, it is more common to return Boolean values from Boolean expressions, for conditional testing. The Boolean value of an expression is the basis for all Java comparisons and conditions.
If statements with Boolean keyword in Java
From the given example, we have declared two variables (‘x1’ and ‘y1’) with various values. Then, we initialized two Boolean variables (‘x2’ and ‘y2’) implementing the value “true” and “false”.
We achieve if statement in which we declared a conditional check to preprint the statement that is mentioned in the block.
public class ex
{
public static void main (String [] s)
{
// declare 'x1' and 'y1'
int x1 = 10;
int y1 = 20;
// implement Boolean variables 'x2' and 'y2'
Boolean x2 = true;
Boolean y2 = false; /* * if condition initiated here. If this condition
Matches
* then 'x2' will be printed. */
if (y > x)
{
System.out.println(x2);
} /* * if this condition matches then 'y2' will be printed */
Else
{
System.out.println(y2);
}
}
}
Output:
true
Examples of Java Boolean keyword
Example 1: Simple Boolean example
public class BooleanEx1 {
public static void main (String [] s)
{
int n1=100;
int n2=200;
Boolean c1=true;
Boolean c2=false;
if(n1<n2)
{
System.out.println(c1);
}
else
{
System.out.println(c2);
}
}
}
Test it Now
Output:
true
Example 2: Comparing the variables of Boolean type
public class BooleanEx2 {
public static void main(String [] s) {
Boolean c1=true;
Boolean c2=false;
Boolean c3=(c1==c2);
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
}
}
Test it Now
Output:
x
true
false
false
Example 3: Boolean type method
public class BooleanEx3 {
public static booleandisplay (int n)
{
if(n>20)
{
return true;
}
else
{
return false;
}
}
public static void main (String [] s) {
System.out.println(display (15));
}
}
Output:
false
Example 4: Comparing objects using Boolean of conditional statements
public class BooleanEx4 {
public static void main (String [] s) {
BooleanEx4 f1=new BooleanEx4();
BooleanEx4 f2=new BooleanEx4();
Boolean ans=(f1==f2);
System.out.println("both objects are equal or not: "+ans);
}
}
Output:
Both objects are equal or not: false
Example 5: Finding a prime number
public class BooleanEx5 {
public static void main(String [] s) {
int n1=7;
Boolean host=false;
for (int i=2; i<n1;i++)
{
if(n1%i==0)
{
host=true;
break;
}
}
if(host)
{
System.out.println("Not a prime number");
}
else
{
System.out.println("prime number");
}
}
}
Output:
prime