Home » Java programming language

Java Boolean class equals() method with example

Boolean class equals() method: Here, we are going to learn about the equals() method of Boolean class with its syntax and example.
Submitted by Preeti Jain, on October 25, 2019

Boolean class equals() method

  • equals() method is available in java.lang package.
  • equals() method is used to check equality or inequality of this Object against the given Object or in other words we can say this method is used to compare two objects.
  • equals() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • equals() method does not throw an exception at the time of comparing two Objects.

Syntax:

    public boolean equals(Object ob);

Parameter(s):

  • Object ob – represents the Object to compare with.

Return value:

The return type of this method is boolean - it returns a boolean value based on the following cases,

  • It returns true, if Object1 is equal to Object2.
  • It returns false, if Object1 is not equal to Object2.

Example:

// Java program to demonstrate the example 
// of boolean equals(Object o) method of Boolean class

public class EqualsOfBooleanClass {
    public static void main(String[] args) {
        boolean b1 = true;
        boolean b2 = true;

        // Boolean Object initialization
        Boolean ob1 = new Boolean(b1);
        Boolean ob2 = new Boolean(b2);

        // Display ob1,ob2 values
        System.out.println("ob1: " + ob1);
        System.out.println("ob2: " + ob2);

        // It compare two objects of Boolean type
        // by calling ob1.equals(ob2)

        boolean compare = ob1.equals(ob2);

        // Display result values
        System.out.println("ob1.equals(ob2): " + compare);

        if (compare == true)
            System.out.println("Both objects are equal");
        else
            System.out.println("Both objects are not equal");
    }
}

Output

ob1: true
ob2: true
ob1.equals(ob2): true
Both objects are equal





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.