equals与“==”操作符的比较

2016-02-19 13:23 2 1 收藏

在这个颜值当道,屌丝闪边的时代,拼不过颜值拼内涵,只有知识丰富才能提升一个人的内在气质和修养,所谓人丑就要多学习,今天图老师给大家分享equals与“==”操作符的比较,希望可以对大家能有小小的帮助。

【 tulaoshi.com - 编程语言 】


  equals与"=="操作符的比较
  
  --------------------------------------------------------------------------------
  
  
  
  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方法和 = =操作符是完成了相同的比较功能,都是对对象的引用进行了比较。那么我们熟悉的String类的equals方法是对什么内容进行比较的呢?下面我们来看它的代码和注释:
  源代码:
  -------------------------------------------------------------
  public boolean equals(Object anObject) {
   if (this == anObject) {
   return true;
   }
   if (anObject instanceof String) {
   String anotherString = (String)anObject;
   int n = count;
   if (n == anotherString.count) {
   char v1[] = value;
   char v2[] = anotherString.value;
   int i = offset;
   int j = anotherString.offset;
   while (n-- != 0) {
   if (v1[i++] != v2[j++])
   return false;
   }
   return true;
   }
   }
   return false;
  }
  
  -------------------------------------------------------------
  此方法的注释为:
  -------------------------------------------------------------
  Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
  -------------------------------------------------------------
  由上面的代码和注释可以得到String类的equal方法是对对象的值进行比较。
  根据以上的讨论可以得出结论:equal方法和= =操作符是否存在区别要个别对待,要根据equal的每个实现情况来具体判定。
  *******************************
  
  belter(belter@sina.com)
  
  none.blogdriver.com

来源:https://www.tulaoshi.com/n/20160219/1604078.html

延伸阅读
标量(scalar)数据类型 标量(scalar)数据类型没有内部组件,他们大致可分为以下四类: . number . character . date/time . boolean 表1显示了数字数据类型;表2显示了字符数据类型;表3显示了日期和布尔数据类型。 表1 Scalar Types:Numeric Datatype Range Subtypes description BINARY_INTEGER...
与C一样,C++使用布尔表达式简化求值法(short-circuit evaluation)。这表示一旦确定了布尔表达式的真假值,即使还有部分表达式没有被测试,布尔表达式也停止运算。例如: char *p; ... if ((p != 0) && (strlen(p) 10)) ... 这里不用担心当p为空时strlen无法正确运行,因为假如p不等于0的测试失败,st...
1. ReferenceEquals, == , EqualsEquals , == , ReferenceEquals都可以用于判断两个对象的个体是不是相等。a) ReferenceEqualsReferenceEquals是Object的静态方法,用于比较两个引用类型的对象是否是对于同一个对象的引用。对于值类型它总是返回false。(因为Box以后的对象总是不同的,hehe)b) ==是一个可以重载的二元操作符,可以用于比较两个...
C++ 中重载 + 操作符的正确方法 作者:Danny Kalev 编译:MTT 工作室 原文出处:Overloading Operator + the Right Way 摘要: 本文概要性地介绍如何选择正确的策略来为用户定义类型重载 + 操作符。 用户...
标签: PHP
三、 使用"instanceof"操作符 如你所见,"instanceof"操作符的使用非常简单,它用两个参数来完成其功能。第一个参数是你想要检查的 对象 ,第二个参数是类名(事实上是一个接口名),用于确定是否这个对象是相应类的一个实例。当然,我故意使用了上面的术语,这样你就可以看到这个操作符的使用是多么直观。它的基本语法如...

经验教程

966

收藏

58
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部