當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。