Java.math.BigInteger.equals() Method



The java.math.BigInteger.equals(Object x) compares this BigInteger with the specified Object for equality.


Following is the declaration for java.math.BigInteger.equals() method

public boolean equals(Object x)


equals in class Object


  • x - Object to which this BigInteger is to be compared

Return Value

This method returns true if and only if the specified Object is a BigInteger whose value is numerically equal to this BigInteger.


  • NA


The following example shows the usage of math.BigInteger.equals() method

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

public static void main(String[] args) {

        // create 2 BigInteger objects
        BigInteger bi1, bi2;

        bi1 = new BigInteger("123");
        bi2 = new BigInteger("123");

        // create 2 boolean objects
        Boolean b1, b2;

        // compare bi1 with bi2
        b1 = bi1.equals(bi2);

	// compare bi1 with an object value 123, which is not a BigIntger
        b2 = bi1.equals("123");

        String str1 = bi1 + " equals BigInteger " + bi2 + " is " +b1;
	String str2 = bi1 + " equals object value 123 is " +b2;

	// print b1, b2 values
        System.out.println( str1 );
        System.out.println( str2 );

Let us compile and run the above program, this will produce the following result:

123 equals BigInteger 123 is true
123 equals object value 123 is false