Java Object Class boolean equals(Object o)方法与示例

对象类布尔值等于(对象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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值