equals() 方法是java.lang 包下Integer 类的一个方法。此方法将参数的值与当前 Integer 对象的值进行比较。它返回布尔值(True 或 False),它对应于这个 Integer 和方法参数对象的相等性。它还覆盖了 Object 类的 equals() 方法。
用法:
以下是 equals() 方法的声明:
public boolean equals(Object obj)
参数:
数据类型 | 参数 | 描述 | 必需/可选 |
---|---|---|---|
Object | obj | 它检查与指定整数的相等性 | Required |
返回值:
如果参数不为空并且整数对象与方法参数对象相同,则 equals() 方法将返回真,否则将返回假。
异常:
输入不匹配异常
兼容版本:
Java 1.2 及以上
例子1
public class IntegerEqualsExample1 {
public static void main(String[] args) {
Integer obj1 = new Integer(43);
Integer obj2 = new Integer(78);
System.out.print("obj1 and obj2 are equal. True or False? = ");
System.out.println(obj1.equals(obj2));
obj1 = new Integer(55);
obj2 = new Integer(55);
System.out.print("obj1 and obj2 are equal. True or false? = ");
System.out.println(obj1.equals(obj2));
}
}
输出:
obj1 and obj2 are equal. True or False? = false obj1 and obj2 are equal. True or false? = true
例子2
import java.util.Scanner;
public class IntegerEqualsExample2 {
public static void main(String[] args) {
Scanner readInput = new Scanner(System.in);
System.out.print("Input the first Integer Number to be Compare:");
Integer intComp1 = readInput.nextInt();
System.out.print("Input the second Integer Number to be Compare:");
Integer intComp2 = readInput.nextInt();
boolean Result = intComp1.equals(intComp2);
if (Result){
System.out.println("Both the Integer numbers are same...");
}
else {
System.out.println("Both the Integer numbers are different...");
}
readInput.close();
}
}
输出:
1. Input the first Integer Number to be Compare:34 Input the second Integer Number to be Compare:34 Both the Integer numbers are same... 2. Input the first Integer Number to be Compare:45 Input the second Integer Number to be Compare:87 Both the Integer numbers are different...
例子3
public class IntegerEqualsExample3 {
public static void main(String[] args) {
Integer objA = 20;
Integer objB = 20;
Integer objC = 10;
System.out.println("objA == objB? " + objA.equals(objB));
System.out.println("objB == objC? " + objB.equals(objC));
System.out.println("objA == objC? " + objA.equals(objC));
System.out.println("objC == objA? " + objC.equals(objA));
System.out.println("objB == objA? " + objB.equals(objA));
}
}
输出:
objA == objB? true objB == objC? false objA == objC? false objC == objA? false objB == objA? true
示例 4
public class IntegerEqualsExample4 {
public static void main(String[] args) {
Float ObjFloat = 55.55f;
Double ObjDouble = 55.55d;
System.out.println("ObjFloat == ObjDouble? " +ObjFloat.equals(ObjDouble));
}
}
输出:
ObjFloat == ObjDouble? false
相关用法
- Java Integer doubleValue()用法及代码示例
- Java Integer max()用法及代码示例
- Java Integer intValue()用法及代码示例
- Java Integer floatValue()用法及代码示例
- Java Integer toUnsignedLong()用法及代码示例
- Java Integer parseUnsignedInt()用法及代码示例
- Java Integer reverseBytes()用法及代码示例
- Java Integer numberOfLeadingZeros()用法及代码示例
- Java Integer shortValue()用法及代码示例
- Java Integer compare()用法及代码示例
- Java Integer byteValue()用法及代码示例
- Java Integer min()用法及代码示例
- Java Integer getInteger()用法及代码示例
- Java Integer reverseBytes(int i)用法及代码示例
- Java Integer toUnsignedString()用法及代码示例
- Java Integer compareTo()用法及代码示例
- Java Integer numberOfTrailingZeros()用法及代码示例
- Java Integer remainderUnsigned()用法及代码示例
- Java Integer decode()用法及代码示例
- Java Integer signum()用法及代码示例
注:本文由纯净天空筛选整理自 Java Integer equals() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。