快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

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的每个实现情况来详细判定。
返回类别: 教程
上一教程: 用JSP如何读取ACCESS数据库
下一教程: JAVA 语法介绍(六):循环

您可以阅读与"EQUALS与"相关的教程:
· ==和equals是很有区别的
· Java String 的 equals() 方式可能的优化
· 学习笔记之 EQUALS() VS. == 和STRING对象的2种创建过程
· java中两个字符串“Equals”和“==”的区别
· JAVA中两个字符串“EQUALS”和“==”的区别
    微笑服务 优质保证 索取样品