大多数学生都是从C++学起的,大家习惯用C++的==来比较两个string是否相等。为什么Java就要重写equals函数来比较两个String类是否相等呢?(叹气)其实是因为C++的string已经重载了操作符==(如果你还记得重写friendly operation==来比较两个类相等);但是在Java中操作符==比较的是对象指向的虚拟地址(不同对象当然地址不一样),而且Java没有提供操作符的重载操作。以下是String的equals()函数的重写:
/**
* Compares this string to the specified object. The result is {@code
* true} if and only if the argument is not {@code null} and is a {@code
* String} object that represents the same sequence of characters as this
* object.
*
* @param anObject
* The object to compare this {@code String} against
*
* @return {@code true} if the given object represents a {@code String}
* equivalent to this string, {@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
分析一下上面equals()函数:
(1) 首先比较两个对象的引用是否相等,如果两个引用相等,那么两个对象必然相等。
(2) 其次判断anObject是否是String的一个实例 (instanceof关键字的作用是测试一个对象是否是一个类的实例),如果不是则返回false,如果是,则判断obObject的字符串内容是否和this相同:长度是否相等,内容是否相等。
ps: 使用instanceof关键字的问题:当类A是类B的子类时,使用instanceof关键字重写equals()函数就会不符合java语言规范要求equals()方法的“对称性”特性的要求。对称性(对于任何引用x,y,当且仅当y.equals(x)返回true, x.equals(y)也应该返回true)。
一般我们在设计一个类时,需要重写父类的equals方法,在重写这个方法时,需要按照以下几个规则设计:
1、自反性:对任意引用值x,x.equals(x)的返回值一定为true.
2、对称性:对于任何引用值x,y,当且仅当y.equals(x)返回值为true时,x.equals(y)的返回值一定为true;
3、传递性:如果x.equals(y)=true, y.equals(z)=true,则x.equals(z)=true
4、一致性:如果参与比较的对象没任何改变,则对象比较的结果也不应该有任何改变
5、非空性:任何非空的引用值X,x.equals(null)的返回值一定为false
一般类重写equals()函数
一般类对象比较的话,必然要重写equals()方法(不能重载操作符==。。。)。重写的套路怎么样呢?
先来看个栗子:
父类Person的定义如下:
package object;
public class Person {
private String name;
public String getName(){
return name;
}
public Person(String name) {
this.name = name;
}
@Override
public boolean equals(Object anObject){
if(this==anObject) // 引用相等
return true;
if(anObject==null) // 对象为null
return false;
if(getClass() !=anObject.getClass()) //类是否相同
return false;
Person another = (Person)anObject;
return name.equals(another.getName()); // 域属性相等
}
}
子类Student的定义如下:
package object;
public class Student extends Person{
private int studentId;
public int getStudentId(){
return studentId;
}
public Student(String name, int studentId) {
super(name);
this.studentId = studentId;
}
@Override
public boolean equals(Object anObject){
if(!super.equals(anObject)) // 先判断父类的域内容是否相等
return false;
Student another = (Student)anObject; // 判断子类域属性相等
return studentId==another.getStudentId();
}
}
测试类如下:
package object;
public class TestEquals {
public static void main(String args[]) {
// 没有继承的类equals()函数测试
Person pa = new Person("Mike");
Person pb = new Person("Mike");
System.out.println("pa==pb:"+(pa==pb));
System.out.println("pa is equals to pb:"+(pa.equals(pb)));
// 含继承的类equals()函数测试
Student sa = new Student("Alice",123);
Student sb = new Student("Alice",123);
System.out.println("sa==sb:"+(sa==sb));
System.out.println("sa is equals to sb:"+(sa.equals(sb)));
}
}
pa==pb:false
pa is equals to pb:true
sa==sb:false
sa is equals to sb:true
现在来总结一下编写equals()方法建议(参考Java核心技术)
(1) 检测this与anObject是否引用同一个对象:
if(this==anObject) // 引用相等
return true;
如果指向同一个实例,则直接返回true;
(2) 检测anObject是否为null,如果是null,则返回false;
(3) 比较this与anObject是否属于同一个类。
如果equals()的语义在每个子类中有所改变,就用getClass()检测:
if(getClass() !=anObject.getClass()) //类是否相同
return false;
如果所有子类都有统一的语义,就用instanceof检测:
if (anObject instanceof String) {
...
}
(4) 将anObject转换为相应类的类型,然后比较所有域属性的值是否相等(调用各域属性的equals()函数)。
(5) 如果在子类中重新定义equals函数,就要在先调用super.equals(anObject);
public boolean equals(Object anObject){
if(!super.equals(anObject)) // 先判断父类的域内容是否相等
return false;
Student another = (Student)anObject; // 判断子类域属性相等
return studentId==another.getStudentId();
}
如果不太懂(4)到底说的是神马,请看以下摘抄的一篇博文的部分内容,对比instanceof和getClass()的区别,其实getClass会判断实例的类是否相等,而instanceof相等的条件,包括两种情况:(1) 类自身 instanceof 类自身;(2) 子类 instanceof 父类;
class A { }
class B extends A { }
Object o1 = new A();
Object o2 = new B();
o1 instanceof A => true
o1 instanceof B => false
o2 instanceof A => true // <================ HERE
o2 instanceof B => true // <span style="font-family: Arial, Helvetica, sans-serif;"><================ HERE </span>
o1.getClass().equals(A.class) => true
o1.getClass().equals(B.class) => false
o2.getClass().equals(A.class) => false // <===============HERE
o2.getClass().equals(B.class) => true <span style="font-family: Arial, Helvetica, sans-serif;"> // </span><span style="font-family: Arial, Helvetica, sans-serif;"><================ HERE </span>
getClass() will be useful when you want to make sure your instance is NOT a subclass of the class you are comparing with.
参考博客:http://www.jb51.net/article/36114.htm
http://www.cnblogs.com/yxnchinahlj/archive/2010/09/27/1836556.html