对象类布尔值等于(对象o) (Object Class boolean equals(Object o))
This method is available in package java.lang.Object.equals(Object o).
软件包java.lang.Object.equals(Object o)中提供了此方法。
This method is used to check the object with the specified object.
此方法用于检查具有指定对象的对象。
This method returns true if both Object reference and value are the same else return false.
如果Object引用和value相同,则此方法返回true,否则返回false。
Syntax:
句法:
boolean equals(Object o){
}
Parameter(s):
参数:
We pass only one object as a parameter in the method of the Object.
在对象的方法中,我们仅传递一个对象作为参数。
Return value:
返回值:
The return type of this method is boolean that means this method returns true if the reference and value of the object are the same.
此方法的返回类型为boolean ,这意味着如果对象的引用和值相同,则此方法返回true。
Java程序演示对象类equals()方法的示例 (Java program to demonstrate example of Object Class equals() method)
import java.lang.Object;
public class ObjectClass {
public static void main(String[] args) {
// Create a new object of Integer type
Integer in1 = new Integer(10);
// Create a new object of Float type
Float fl = new Float(10.f);
// Create another object of Integer type
Integer in2 = new Integer(20);
// Create another object of Integer type
Integer in3 = new Integer(10);
// Compare Integer and float type reference and value
// and it returns true if both same else false
System.out.println("The result of comparing Integer and Float using equals():" + in1.equals(fl));
// Compare Integer and another Integer type reference
// and value and it returns true if both same else false
System.out.println("The result of comparing Integer and another Integer using equals():" + in1.equals(in2));
// Compare Integer and another Integer type reference
// and value and it returns true if both same else false
System.out.println("The result of comparing Integer and another Integer using equals():" + in1.equals(in3));
}
}
Output
输出量
D:\Programs>javac ObjectClass.java
D:\Programs>java ObjectClass
The result of comparing Integer and Float using equals():false
The result of comparing Integer and another Integer using equals():false
The result of comparing Integer and another Integer using equals():true
翻译自: https://www.includehelp.com/java/object-class-boolean-equals-object-o-method-with-example.aspx