×

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 &gt; 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

Related Topics

Java Math floorMod() Method

The floorMod() method of Math class returns the floor modulus of the specified arguments. It firstly divides the dividend and divisor and then returns an integer that is equal to...

1 minute read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

MOOD Factors to Assess a Java Program

In this tutorial, we will comprehendthe meaning of mood factors in Java. For the development of any software system,the quality of anapplication is important. It is more important to maintain large-scale...

4 minutes read.

Why String in Immutable in Java?

Why String in Immutable in Java Immutable means unchangeable or unmodifiable.  Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change...

2 minutes read.

Parallel Arrays Sort in Java

Sorting is a technique of arranging a sequence of numbers in ascending order, that is, the first number being lowest and gradually incrementing the numbers such that the last number...

7 minutes read.

Different Ways to Take Input from User in Java

Any information provided to a system for use is known as user input. Any responsive software or application must include user input. In Java, there are 4 different ways to...

5 minutes read.

Java Math with Methods and Examples

Java Math class contains various methods for performing math operations like min(), max(), avg() and various trigonometric functions like sin(), cos(), tan() etc. Methods: The java.lang.Math class contains various methods for performing...

5 minutes read.

Zigzag Traversal of Binary Tree in Java

In this article, you will be acknowledged about the zigzag traversal of binary tree in java and the approaches or ways in which the zigzag traversal can be done. Zigzag Traversal A...

10 minutes read.

Computing Digit Sum of all Numbers from 1 to n in Java

In this tutorial, we will discuss various methods to compute the sum of digits of all numbers beginning from 1 to n through a Java program. We will achieve it through...

7 minutes read.

Java Variable Declaration

In this article, you will be acknowledged about java variable declaration. You will be able to learn and interpret about declaring a variable in Java. Variable in Java Variables are necessary for...

6 minutes read.

Factorial Program in Java using Recursion

Factorial Program in Java Factorials are used in mathematics to calculate permutations and combinations. It is denoted by the exclamatory symbol (!). Suppose, p is a number whose factorial is to...

3 minutes read.

How to Convert double to int in Java

The double is a larger data type than int. When we assign a larger type value to a variable of smaller type, then we need to perform the explicit conversion....

2 minutes read.

Java HashMap

Java HashMap extends AbstractMap and implements Map interface. It is the collection of multiple entries where an entry consists of key and value pair. The HashMap can contain only one...

7 minutes read.

Java Math subtractExact() Method

The subtractExact() method of Java Math class returns mathematical difference of the specified two arguments, throwing an exception if the result overflows int or long. Syntax: public static int subtractExact (int x,...

1 minute read.

Java Database Connectivity with Oracle

JDBC: A Programmer can develop a complete application using the Java built-in API’s. So, for storing the data required for solving a real-world problem is stored into a database. To connect...

5 minutes read.

Differences between Set and List in Java

Set in Java: The Java. util package contains an interface called the set. The set interface expands the Collection interface. A collection interface is an unordered collection of List where duplicates...

6 minutes read.

Static class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM is mostly...

7 minutes read.

Java Program to Sort an Array of 0's, 1's, and 2’s | Dutch National Flag Problem in Java

The famed Dutch computer programmer Edsger Dijkstra's Dutch Nation Flag (DNF) challenge ranks among the most well-known programming challenges. The Dutch tricolor flag, comprising red, white, and blue, is the...

3 minutes read.

Java 13 New Features

The Java SE Platform's JDK 13 version is an open-source reference implementation defined by the Java Community's JSR 388 Process. The necessary modifications made in Java 13 are listed below....

6 minutes read.

How annotations work in Java

Annotations were introduced by sun microsystem and are available from the java 1.5 version. In general, configurations can be done either by XML or by annotations. Hibernate and spring framework users prefer...

4 minutes read.