Java BigInteger Class | equals() Method with Example

BigInteger Class equals() method: Here, we are going to learn about the equals() method of BigInteger Class with its syntax and example.
Submitted by Preeti Jain, on May 10, 2020

BigInteger Class equals() method

  • equals() method is available in java.math package.
  • equals() method is used to check whether this BigInteger and the given object are equal or not.
  • 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 is to be compared to this object.

Return value:

The return type of this method is boolean, it returns true when both the compared objects are equal in value and type otherwise it returns false.

Example:

// Java program to demonstrate the example 
// of boolean equals(Object ob) method of BigInteger

import java.math.*;

public class EqualsOfBI {
    public static void main(String args[]) {
        // Initialize two variables str1 and str2

        String str1 = "12312";
        String str2 = "14502";
        String str3 = "12312";

        // Initialize two BigInteger objects  
        BigInteger b_int1 = new BigInteger(str1);
        BigInteger b_int2 = new BigInteger(str2);

        // Display b_int1 , b_int2 and str3
        System.out.println("b_int1: " + b_int1);
        System.out.println("b_int2: " + b_int2);
        System.out.println("str3: " + str3);

        // compares this BigInteger (b_int1) to the given
        // BigInteger b_int2 for equality and here it returns
        // false because they are not equal in values

        boolean compare = b_int1.equals(b_int2);
        System.out.println("b_int1.equals(b_int2): " + compare);

        // compares this BigInteger (b_int1) to the given
        // BigInteger b_int1 for equality and here it returns
        // true because they are equal in values

        compare = b_int1.equals(b_int1);
        System.out.println("b_int1.equals(b_int1): " + compare);

        // compares BigInteger (b_int1) to the given Object
        // String str3 for equality and here it returns
        // false because they both are equal in values
        // but both the compared object are not of 
        // same "BigInteger" type
        compare = b_int1.equals(str3);
        System.out.println("b_int1.equals(str3): " + compare);
    }
}

Output

b_int1: 12312
b_int2: 14502
str3: 12312
b_int1.equals(b_int2): false
b_int1.equals(b_int1): true
b_int1.equals(str3): false


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.