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)
相关用法
- Java BigInteger mod()用法及代码示例
- Java BigInteger abs()用法及代码示例
- Java BigInteger pow()用法及代码示例
- Java BigInteger or()用法及代码示例
- Java BigInteger not()用法及代码示例
- Java BigInteger xor()用法及代码示例
- Java BigInteger and()用法及代码示例
- Java BigInteger compareTo()用法及代码示例
- Java BigInteger valueOf()用法及代码示例
- Java BigInteger hashCode()用法及代码示例
- Java BigInteger doubleValue()用法及代码示例
- Java BigInteger intValue()用法及代码示例
- Java BigInteger remainder()用法及代码示例
- Java BigInteger toByteArray()用法及代码示例
- Java BigInteger longValue()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 BigInteger equals() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。