×

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

Morris Traversal for Inorder in Java

Through Morris’s traversal, a tree is traversed without the aid of recursion or stacks. Based on the threaded binary tree, the Morris traversal is used. We perform internal modification throughout...

4 minutes read.

Empty Statement in Java

The three sorts of statements in Java are control, expression, and declaration statements. Additionally, another statement is referred to as an empty statement. In this section, we will discuss about...

4 minutes read.

Java Math ulp() Method

The ulp() method of Java Math class returns the size of an ulp of the argument. Syntax: public static double ulp(double x)public static float ulp(float x) Parameters: The parameter ‘x’ represents the floating-point number...

2 minutes read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.

Java While Keyword

Depending on a specified Boolean condition, a while loop in Java allows code to be executed repeatedly. The while loop can be viewed as an iterative version of the if...

3 minutes read.

Java Destructors

Before knowing about Java destructors, let us know about constructors. If we understand the concept of the constructor, we can easily understand about destructor and also, we will get an...

3 minutes read.

Difference Between Data Hiding and Abstraction in Java

Abstraction: Data Abstraction and Data Hiding ideas are utilised to show the expected data to the end client and conceal the superfluous subtleties, however, for specific purposes like decreasing the framework's...

10 minutes read.

Java Generate Random String

Java Generate Random String In this tutorial, we will learn about to generate random string in Java. Random generation strings mean any string will be generated, which does not follow any...

7 minutes read.

Java RMI

What is RMI in Java? A framework for developing distributed Java applications is provided by the RMI (Remote Method Invocation) API. An object can call methods on an object running in...

4 minutes read.

Java.lang.Exception.NoRunnableMethods

In Programming language, the java lang unexpected no precompiled methods error generally refers to a Junit exception that happens whenever Junit is incapable of locate the precompiled test methods. When...

4 minutes read.

Compile-time Error in Java

In java, the execution of a program is stopped due to the occurrence of some problem known as an error. Errors are illegal operations that are carried out by the...

4 minutes read.

Java Print Writer

PrintWriter: It is used to write output data from the file. It is the class of Java.io package. It inherits the properties of the Writer class. Writer Class: It is a class of...

4 minutes read.

Java AWT

Java AWT Java programming is used to develop different types of applications like window-based applications, web applications, Enterprise applications, or mobile applications. For creating standalone applications, Java AWT API is used....

11 minutes read.

Java LinkedList vs ArrayList

LinkedList In LinkedList, each element is a distinct entity containing an information portion and an address component, and the elements are not kept in consecutive locations. Pointers & addresses are used...

3 minutes read.

Difference Between Thread.start() and Thread.run()

In the Java programming language, the multi-threading concept consists of the start() and run() methods. Thread.start(): The thread's execution is initiated by invoking the start() method. The start() method operates two threads...

4 minutes read.

Java Math nextDown() Method

The nextDown() method of Math class returns the floating-point number adjacent to the argument in direction of the negative infinity. Syntax: public static double nextDown (double d)public static float nextDown (float f) Parameters: The...

2 minutes read.

Java String Interview Questions

Java String Interview Questions String is the most common and important discussed topics in a Java interview. In Java interview interviewer generally asked three to four questions based on String concept....

16 minutes read.

Iterate JSON array in Java

This article is going to equip the knowledge about what JSON array is and how it works and also how is it distinct with the generic array. Json Array Ordered lists of...

4 minutes read.

Java Tutorial

What is Java? Java is an object-oriented, robust, secured and platform-independent programming language. With the help of Java Programming, we can develop console, window, web, enterprise and mobile applications. Java language was...

22 minutes read.

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.