@frank-shaw
2015-10-31T11:11:00.000000Z
字数 2648
阅读 3002
java.基础知识
回答这个问题之前,首先需要弄清楚该变量类型是什么?
变量类型如果是值类型,那么它存储在内存中的虚拟机栈中。而引用类型的变量在栈中仅仅是存储引用类型变量的地址,而被引用者本身则存储在堆中。
==操作比较的是两个变量的值是否相等,对于引用型变量表示的是两个变量在堆中存储的地址是否相同,即栈中的内容是否相同。
对于引用型变量,equals操作表示的两个变量所引用的对象是否相同,即堆中的内容是否相同(默认情况下Object.equals(),两个引用型变量一定要指向同一个对象,看看源码就知道了。但是其他类就不是这样子了,毕竟没个类都有不同的内容,还是需要跟内容来看是否相等的)。==比较的是2个对象的地址,而equals比较的是2个对象的内容。
我们可以看一下一个很简单的代码,分析下面的Person类的equals()方法是怎样做判断的:
class Person{private int age;private String name;public Person() {}public Person(int age, String name) {this.age = age;this.name = name;}@Overridepublic boolean equals(Object obj) {//判断两个对象的内存地址是否相同,相同则返回真if (this == obj)return true;if (obj == null)return false;//判断两个对象是否是同一个类if (getClass() != obj.getClass())return false;//在判断得到两个对象是同一个类的基础上,进而来判断内容Person other = (Person) obj;if (age != other.age)return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true;}}
可以看到,上面的equals()方法的判断中使用到了“==”,表示的是判断两个对象的地址是否相等。即使两对象不是同一个对象,那么也可以看看两个对象是否是同一个类,在同一个类的基础上,那么就可以比较其中的内容是否相等。这就是Person类判断是否相等的一个逻辑。在判断其中的内容是否相等的过程中,由于name 是String类,其用到了String类的equals()方法。接下来让我们来看看源代码String类的equals()方法是怎样的:
public final class Stringimplements java.io.Serializable, Comparable<String>, CharSequence {/** The value is used for character storage. */private final char value[];/** Cache the hash code for the string */private int hash; // Default to 0/** use serialVersionUID from JDK 1.0.2 for interoperability */private static final long serialVersionUID = -6849794470754667710L;/*** Class String is special cased within the Serialization Stream Protocol.** A String instance is written into an ObjectOutputStream according to* <a href="{@docRoot}/../platform/serialization/spec/output.html">* Object Serialization Specification, Section 6.2, "Stream Elements"</a>*/private static final ObjectStreamField[] serialPersistentFields =new ObjectStreamField[0];······/*** 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) {//两个对象的地址是否相同,如果相同则返回trueif (this == anObject) {return true;}//判断anObject对象是否是String类,若不是返回falseif (anObject instanceof String) {String anotherString = (String)anObject;//再比较value数组的各个值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()方法都是可以我们认为定义的。何为相等,你定义的相等即为相等。就是这么简单。
我尝试着多去看源码,这样子可以学到更多的东西,领略大师写代码都是怎样写的。