== 和 equals方法的区别
== 是一个 比较运算符,既可以比较基本数据类型,也可以比较引用数据类型。基本数据类型比较的是值,引用数据类型比较的是地址。
equals 方法是一个 方法,只能比较引用数据类型,所有的对象都会继承object类中的方法,如果没有重写object类中的equals方法,equals方法和== 比较引用数据类型没有区别,重写后的equals方法比较的是对象中的属性
写个栗子作比较:
public class Simple {
public static void main(String[] args) {
int x = 1;
int y = 1;
int[] arr1 = new int[10];
int[] arr2 = arr1; //arr2与arr1同时指向刚开辟的数组
if(x == y) { //==比较基本数据类型
System.out.println("x = "+x+ " " +"y = "+y);
}
if(arr1 == arr2) { //==比较引用数据类型
System.out.println("arr1 = "+arr1);
System.out.println("arr2 = "+arr2);
}
if(arr1.equals(arr2)) { //equals比较引用数据类型
System.out.println("arr1 = "+arr1);
System.out.println("arr2 = "+arr2);
}
}
}
arr1和arr2数组打印的是哈希地址
翻来equals的源码(没有重写):
/**
* Indicates whether some other object is "equal to" this one.
* <p>
* The {@code equals} method implements an equivalence relation
* on non-null object references:
* <ul>
* <li>It is <i>reflexive</i>: for any non-null reference value
* {@code x}, {@code x.equals(x)} should return
* {@code true}.
* <li>It is <i>symmetric</i>: for any non-null reference values
* {@code x} and {@code y}, {@code x.equals(y)}
* should return {@code true} if and only if
* {@code y.equals(x)} returns {@code true}.
* <li>It is <i>transitive</i>: for any non-null reference values
* {@code x}, {@code y}, and {@code z}, if
* {@code x.equals(y)} returns {@code true} and
* {@code y.equals(z)} returns {@code true}, then
* {@code x.equals(z)} should return {@code true}.
* <li>It is <i>consistent</i>: for any non-null reference values
* {@code x} and {@code y}, multiple invocations of
* {@code x.equals(y)} consistently return {@code true}
* or consistently return {@code false}, provided no
* information used in {@code equals} comparisons on the
* objects is modified.
* <li>For any non-null reference value {@code x},
* {@code x.equals(null)} should return {@code false}.
* </ul>
* <p>
* The {@code equals} method for class {@code Object} implements
* the most discriminating possible equivalence relation on objects;
* that is, for any non-null reference values {@code x} and
* {@code y}, this method returns {@code true} if and only
* if {@code x} and {@code y} refer to the same object
* ({@code x == y} has the value {@code true}).
* <p>
* Note that it is generally necessary to override the {@code hashCode}
* method whenever this method is overridden, so as to maintain the
* general contract for the {@code hashCode} method, which states
* that equal objects must have equal hash codes.
*
* @param obj the reference object with which to compare.
* @return {@code true} if this object is the same as the obj
* argument; {@code false} otherwise.
* @see #hashCode()
* @see java.util.HashMap
*/
public boolean equals(Object obj) {
return (this == obj); //底层利用的还是==
}
compare_equals类重写了equals方法(用的是Alt+shift+s自动生成):
public class compare_equals {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
compare_equals other = (compare_equals) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))//判断的是属性是否相等
return false;
return true;
}
}
在Simple类中创建compare_equals的实例:
import compare_equals;
public class Simple {
public static void main(String[] args) {
compare_equals com1 = new compare_equals();
com1.setName("张三");
compare_equals com2 = new compare_equals();
com2.setName("张三");
if(com1.equals(com2)) {
System.out.println(com1);
System.out.println(com2);
}else {
System.out.println("ERROR");
}
}
}
解析:com1和com2 一共new了两个实例,分配是不同的内存,所以二者的hash地址肯定不同,但由于重写了equals方法,比较的是对象的属性值即都是“张三”,因此会把两个对象的地址打印出来(不同的哈希地址)而不是打印ERROR。
打印结果: