当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java BigInteger equals()用法及代码示例


java.math.BigInteger.equals(Object x)方法将此BigInteger与作为参数传递的对象进行比较,并且两者的返回值相等时返回true,否则返回false。

用法:

public boolean equals(Object x)

参数:此方法接受单个强制参数x,该参数是与BigInteger对象进行比较的对象。


返回值:当且仅当作为参数传递的Object是BigInteger,且其值等于应用该方法的BigInteger Object时,此方法才返回boolean true。否则它将返回false。

例子:

Input: BigInteger1=2345, BigInteger2=7456
Output: false
Explanation: BigInteger1.equals(BigInteger2)=false.

Input: BigInteger1=7356, BigInteger2=7456
Output: true
Explanation: 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)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 BigInteger equals() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。