×

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 extend multiple classes

In Java, what does extend mean? One of several Java inheritance keywords is extended, meaning we pass all or most of the Parent class's characteristics through to the Child class. The...

3 minutes read.

XOR of Array Elements Except Itself in Java

A lot of the biggest IT organisations, including The Google, Amazon, TCS, and The Accenture, etc., question about this issue in their interview processes. By addressing the issue, one hopes to assess...

5 minutes read.

How to uninstall the Java in Ubuntu

We need java on our computers in our daily lives because there are lots of different applications that are developed using the java environment, so we have to install java...

4 minutes read.

Java Enum vs Class

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four integrators named Club, Diamond,...

7 minutes read.

Java Clone Array

We frequently need to copy an array to back up its original components. We have a few unique numbers and strings, including Armstrong numbers, palindrome numbers, and palindrome strings. To...

4 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Lazy Propagation in Segment Tree in Java

The topic of segment trees in Java is continued by the topic of sluggish propagation in segment trees. It is suggested that readers first read through the section tree topic....

4 minutes read.

Selection Sort in Java

Selection Sort in Java Selection sort is also a simple sorting algorithm that works by repeatedly finding the minimum element from the unsorted portion of the input array, and placing it...

5 minutes read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Buffer reader to read string in Java

The Buffered Reader class of Java is used to read the stream of characters from the input stream. Program to read string using Buffer reader import java.io.*; class  Demo {   public static void main(String...

3 minutes read.

Java Error Stack Trace

The stack trace in Java is an array of stacks.The stack trace reveals the console's location of an exception or error by gathering data from all program methods. The JVM...

3 minutes read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

5 minutes read.

Class definition in Java

The class definition in Java Java is an object-oriented programming language. We essentially know that programming languages based on object-oriented paradigms have classes and objects in their concepts as main, which...

6 minutes read.

Calculator Program in Java

Calculator Program in Java A calculator performs the basic mathematical operations such as addition, subtraction, multiplication, etc. In this section, we will create a calculator program in Java using switch case...

5 minutes read.

CopyOnWriteArrayList in Java

The CopyOnWriteArrayList is used to implement the List Interface. It is the improved version of ArrayList. The operations like add, remove, set, update etc, these operations are done by creating...

5 minutes read.

Differences between Lock and Monitor in Java Concurrency

In this tutorial, we will discuss the overview of Lock and Monitor and the differences between them. Introduction Java Concurrency is the ability to perform specific tasks at a time parallelly. The...

4 minutes 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.

Java class class

Java Class class instances are an executing Java application's implementation of the classes and interfaces. As well as, every Array is indeed an object that is common for all Arrays...

6 minutes read.

Advanced Java Viva Questions

One of the more difficult languages available now is Java. Currently, 10 thousand developers worldwide use the programming language, which is rising daily. So, if you're a Java developer, an aspiring...

9 minutes read.

Transaction Management in java

Definition: A database application is an application that is running against a relational database and executes one or more transactions. A transaction is an executing program that contains some database operations,...

4 minutes read.