Java BigInteger equals()方法

Java BigInteger equals()方法

java.math.BigInteger.equals(Object x) 方法将这个BigInteger与作为参数传递的对象进行比较,如果两者的值相等,则返回真,否则返回假。

语法

public boolean equals(Object x)

参数: 此方法接受一个强制参数 x ,它是BigInteger对象要被比较的对象。

返回: 当且仅当作为参数传递的对象是一个BigInteger,其值等于应用该方法的BigInteger对象时,该方法返回布尔值true。否则,它将返回false。

例子

输入ut:** BigInteger1=2345, BigInteger2=7456
输出: false
解释: BigInteger1.equals(BigInteger2)=false.

输入: BigInteger1=7356, BigInteger2=7456
输出: true
解释: BigInteger1.equals(BigInteger2)=true.

下面的程序说明了BigInteger类的equals()方法。

例1: 当两者的值相等时。

// Java program to demonstrate equals() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Creating 2 BigInteger objects
        BigInteger b1, b2;
  
        b1 = new BigInteger("321456");
        b2 = new BigInteger("321456");
  
        // apply equals() method
        boolean response = b1.equals(b2);
  
        // print result
        if (response) {
  
            System.out.println("BigInteger1 " + b1
                               + " and BigInteger2 "
                               + b2 + " are equal");
        }
        else {
  
            System.out.println("BigInteger1 " + b1
                               + " and BigInteger2 "
                               + b2 + " are not equal");
        }
    }
}

输出。

BigInteger1 321456 and BigInteger2 321456 are equal

例2: 当两者的价值不相等时。

// Java program to demonstrate equals() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Creating 2 BigInteger objects
        BigInteger b1, b2;
  
        b1 = new BigInteger("321456");
        b2 = new BigInteger("456782");
  
        // apply equals() method
        boolean response = b1.equals(b2);
  
        // print result
        if (response) {
  
            System.out.println("BigInteger1 " + b1
                               + " and BigInteger2 "
                               + b2 + " are equal");
        }
        else {
  
            System.out.println("BigInteger1 " + b1
                               + " and BigInteger2 " + b2 + " are not equal");
        }
    }
}

输出。

BigInteger1 321456 and BigInteger2 456782 are not equal

例3: 当作为参数传递的对象不是BigInteger时。

// Java program to demonstrate equals() method of BigInteger
  
import java.math.BigInteger;
  
public class Main6 {
  
    public static void main(String[] args)
    {
  
        // Creating  BigInteger object
        BigInteger b1;
  
        b1 = new BigInteger("321456");
        String object = "321456";
  
        // apply equals() method
        boolean response = b1.equals(object);
  
        // print result
        if (response) {
  
            System.out.println("BigInteger1 " + b1
                               + " and String Object "
                               + object + " are equal");
        }
        else {
  
            System.out.println("BigInteger1 " + b1
                               + " and String Object "
                               + object + " are not equal");
        }
    }
}

输出。

BigInteger1 321456 and String Object 321456 are not equal

**参考资料: **https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#equals(java.lang.Object)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程