BigInteger类的equals()方法 (BigInteger Class equals() method)
equals() method is available in java.math package.
equals()方法在java.math包中可用。
equals() method is used to check whether this BigInteger and the given object are equal or not.
equals()方法用于检查此BigInteger和给定对象是否相等。
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()方法是一种非静态方法,仅可通过类对象访问,如果尝试使用类名访问该方法,则会收到错误消息。
equals() method does not throw an exception at the time of comparing two objects.
在比较两个对象时, equals()方法不会引发异常。
Syntax:
句法:
public boolean equals(Object ob);
Parameter(s):
参数:
Object ob – represents the object is to be compared to this object.
对象ob –表示要与该对象进行比较的对象。
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.
此方法的返回类型为boolean ,当两个比较对象的值均相等时,返回true,否则返回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
翻译自: https://www.includehelp.com/java/biginteger-equals-method-with-example.aspx