当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:14:00  来源:本站整理

equals与"=="操作符的比较[Java编程]

赞助商链接



  本文“equals与"=="操作符的比较[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

equals办法是Object类的一个办法,全部担当自Object类的类城市集成此办法,并且可以重载这个办法来实现各自的对比操作,并且jdk也恰是举荐这种做法.所以开辟人员尽可以在自己的类中实现自己的equals办法来完成自己特定的对比功效,所以各个类的equals办法与= =之间并没有绝对的关系,这要按照各自类中自己的实现情形来看.也就是说大概会有两种情形发生:equals办法和= =相同大概不相同.在大都情形下这二者的辨别就是毕竟是对对象的引用举行对比还是对对象的值举行对比(其他特别情形此处不予考虑).那么= =操作符是对比的什么呢?= =操作符是对比的对象的引用而不是对象的值.并且由下面的源代码可以看出在最初的Object对象中的equals办法是与= =操作符完成功能是相同的.

源码:

java.lang.Object.equals()办法:

-------------------------------------------------------------

public boolean equalss(Object obj) {
return (this = = obj);
}

-------------------------------------------------------------

jdk文档中给出以下注释:

-------------------------------------------------------------

The equalss method implements an equivalence relation:

? It is reflexive: for any reference value x, x.equalss(x) should return true.

? It is symmetric: for any reference values x and y, x.equalss(y) should return true if and only if y.equalss(x) returns true.

? It is transitive: for any reference values x, y, and z, if x.equalss(y) returns true and y.equalss(z) returns true, then x.equalss(z) should return true.

? It is consistent: for any reference values x and y, multiple invocations of x.equalss(y) consistently return true or consistently return false, provided no information used in equalss comparisons on the object is modified.

? For any non-null reference value x, x.equalss(null) should return false.

The equalss method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equals objects must have equals hash codes.

-------------------------------------------------------------


  以上是“equals与"=="操作符的比较[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:

  • <b>深化equals办法对象相等比较</b>
  • Java:重写equals()和hashCode()
  • Java:全部的equals办法实现都是错误的?
  • equals与"=="操作符的比较
  • 如安在Java中避免equals办法的躲藏陷阱
  • equals(Object) 与 == 的辨别
  • Java中Object类的equals()和hashCode()办法深化解析
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .