×

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

Anonymous Function in Java

A function defined as being unbound from an identifier is called an anonymous function. Because they permit access to variables within the scope of the contained function, these are a...

4 minutes read.

Array Programs in Java

Array Programs in Java: An array is a data structure that stores similar elements in a contiguous memory location. In Java, an array is an object that stores the same...

7 minutes read.

Pancake Sorting in Java

In the pancake sorting method, the array must be sorted using just one operation, which is: Flip the arrays arr at index 0 to index j by using flipArr(arr, h). Other sorting...

3 minutes read.

Callable Statement in Java

The Callable statement in Java is used to call the functions and Stored procedures. Example: If we want to know about the age of a person based on their date of birth,...

3 minutes read.

How to remove special characters from String in Java

Strings in Java are Objects that are supported inside by a burn exhibit. Since exhibits are immutable (cannot develop), Strings are changeless too. A completely new String is made whenever...

5 minutes read.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

Arithmetic Operations on String in Java

Introduction Arithmetic, Relational, Bitwise, and Logical operators are all available in Java. Simple mathematical calculations are performed using Java arithmetic operators. Basic Arithmetic operators are considered in Java to be Addition,...

4 minutes read.

Applet Life Cycle in Java

In this article, we are going to acknowledge you about what a applet is, what is its life cycle and stages in life cycle, along with the syntax and example...

4 minutes read.

RMI program in Java

Remote Method Invocation is what it stands for. An object can call the method of another object in a different space address using the RMI API, which may be on the...

3 minutes read.

Java Short Keyword

Java supports eight different primitive datatypes. The language has predefined primitive datatypes that are given keyword names. Let's take a closer look at each of the eight primitive data types....

3 minutes read.

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Relatively Prime in Java

In this article, you will be very well equipped with a knowledge of a relatively prime number and also Java programs to determine whether a given integer is a relatively...

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

Short Circuit Logical Operators in Java

When there are two or more relational expressions in a decision-making statement, logical operators are utilized to combine them. The logical operators short circuit and not-short circuit fall into two...

5 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.

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.

Session Tracking in Java

When a series of requests from the same User (i.e., requests coming from the same browser) occurs over an extended period of time, servlets employ a mechanism known as session...

3 minutes read.

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String. Using valueOf(boolean) method Using toString(boolean) method For both the above methods, if the passes Boolean...

2 minutes read.

Java String indexOf() method

Java String indexOf() method returns index of a given character or substring present in a String. Methods Description int indexOf(int ch)     It returns index position for the given char value.int indexOf(int ch,...

2 minutes read.

Java List Interface

List interface is used when we have to order a collection which contains duplicate entries. Like an array, the elements of its implementation classes are retrieved and inserted at a...

2 minutes read.