×

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false.

Syntax

public boolean equals(Object obj)

Parameters

The parameter ‘obj’ represents the Boolean object to compare with.

Return Value

This method returns a Boolean value which are as follows:

  • It returns Boolean value true, this object represents the same value
  • It returns Boolean value false, if this Boolean object represents different value.

Example 1

public class JavaBooleanEqualsMethodExample1 {
    public static void main(String[] args) {
        Boolean a = new Boolean(true);
        Boolean b = new Boolean(false);
        // method will check whether a and b are equal or not
         boolean b3=a.equals(b);
             System.out.println("equals() method returns : "+b3);
    }
}

Output:

equals() method returns : false

Example 2

public class JavaBooleanEqualsMethodExample2 {
    static int fact=1;
    public static void main(String[] args) {
        Boolean b1 = new Boolean(true);
        Boolean b2 = new Boolean(true);
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter a no. whose factorial you wanna calculate: ");
        int a=scanner.nextInt();
        if(a<=0 || a>15){
            b2=false;
            System.out.println("Oops! Number out of Reach. \nPlz try a number between 1-15.");
        }
        if (b1.equals(b2)){
            for (int i=a;i>0;i--){
                fact=fact*i;
            }
            System.out.println("Fctorial : "+fact);
        }
    }
}

Output:

Enter a no. whose factorial you wanna calculate: 15
Fctorial : 2004310016

Example 3

public class JavaBooleanEqualsMethodExample3 {
    public static void main(String[] args) {
        //for null values it will pass true
        Boolean b1 = new Boolean(null);
        Boolean b2 = new Boolean(null);
        Boolean b3=b1.equals(b2);
        System.out.println(b3);
    }
}

Output:

true
s

Related Topics

Difference between this and super in Java

In this article, you will be very well equipped with the knowledge of this keyword and super keyword in java. You will be able to understand where to implement this...

3 minutes read.

Math fma () method in java

In java, the Math module constitutes of fma () Method in it. This method can be accessed in two ways which can be differentiated by the parameters which are given...

4 minutes read.

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.

Moran Numbers in Java

In this article, we will be acknowledged about the moran numbers in Java, how they are formed, what are the approaches to achieve the moran numbers. Moran Number A moran numbers are...

3 minutes read.

How to make Java Projects

Ant and Maven are both offered by NetBeans for the development of Java applications. When using Ant, the IDE creates an Ant build script depending on the settings you select...

6 minutes read.

Console Errors in Java

An unlawful motion taken through the person that reasons this system to act abnormally is amistake until this system is compiled or run; maximum programming mistakes pass unnoticed.The software is...

3 minutes read.

Insertion Sort in Java

Insertion Sort in Java Insertion sort in Java is a simple sorting algorithm that works in the same way as we hold cards in hand. Insertion sort does the sorting element-by-element,...

3 minutes read.

Program to Find the Common Elements between two Arrays in Java

In this article, we are going to learn how to find the common elements between two arrays using Java. Here, we use different approaches in Java to find the common...

3 minutes read.

Beautiful Array in Java

In this section, we will be acknowledged about the beautiful array in Java, its characteristics, how it is achieved. What are the types of approaches and what are the tools...

8 minutes read.

Types of Sockets in Java

The fundamental idea behind Java's networking capability is the socket. Early in the 1980s, the Berkeley UNIX 4.2BSD version included the socket paradigm. Berkeley socket is the term employed as...

9 minutes read.

How to download Eclipse for Java

Introduction: We write a java program, and when we want to run it, we need software to run it. Eclipse is a kind of software used to execute a JavaFX...

3 minutes read.

Java Read File Line by line

Java provides two options to read files line by line. Each option offers different benefits and has its own set of drawbacks. Following are the ways through which on accomplish...

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

Java Transient

Java Transient In Java, Serialization is used to convert an object into a stream of the byte. The byte stream consists of the data of the instance as well as the...

3 minutes read.

Java Iterator

When iterating through, traversing, or retrieving the individual elements of a Collection or Flow object, a Java Cursor is leveraged as an iterator. In Java, there exist three types of...

4 minutes read.

Copy data/content from one file to another in java

In this article, you will be acknowledged about how to copy data or content from one file to another file. Also, you will be acknowledged about the classes and methods...

3 minutes read.

Java Open File

Java Desktop class give an open() strategy to open a filr. It has a place with a java.awt package. Work area execution is stage subordinate, so it is important to...

5 minutes read.

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.

Functional Interfaces in Java

Java has forever remained an Object-Oriented Programming language. By object-oriented programming language, we can declare that everything present in the Java programming language rotates throughout the Objects, except for some...

11 minutes read.