最近看了网上有人说,String对象的equals()与==的区别,不知道两者到底有什么区别,如果真的要彻底搞清楚,我们还是从“万类之源”说起,http://blog.itpub.net/29876893/viewspace-1819489/这是之前写关于Object的一篇文章,现在我们还是从源码入手(当然结合API更好)。
下面还是分析Object类中的equals()方法,关于源码如何获取上篇文章已经说的很清楚。
下面是Object类中的equals():
-
public boolean equals(Object obj) {
-
return (this == obj);
-
}
-
-
-
/**
-
* Creates and returns a copy of this object. The precise meaning
-
* of "copy" may depend on the class of the object. The general
-
* intent is that, for any object {@code x}, the expression:
-
*
-
*
-
* x.clone() != x
-
* will be true, and that the expression:
-
*
-
*
-
* x.clone().getClass() == x.getClass()
-
* will be {@code true}, but these are not absolute requirements.
-
* While it is typically the case that:
-
*
-
*
-
* x.clone().equals(x)
-
* will be {@code true}, this is not an absolute requirement.
-
*
-
* By convention, the returned object should be obtained by calling
-
* {@code super.clone}. If a class and all of its superclasses (except
-
* {@code Object}) obey this convention, it will be the case that
-
* {@code x.clone().getClass() == x.getClass()}.
-
*
-
* By convention, the object returned by this method should be independent
-
* of this object (which is being cloned). To achieve this independence,
-
* it may be necessary to modify one or more fields of the object returned
-
* by {@code super.clone} before returning it. Typically, this means
-
* copying any mutable objects that comprise the internal "deep structure"
-
* of the object being cloned and replacing the references to these
-
* objects with references to the copies. If a class contains only
-
* primitive fields or references to immutable objects, then it is usually
-
* the case that no fields in the object returned by {@code super.clone}
-
* need to be modified.
-
*
-
* The method {@code clone} for class {@code Object} performs a
-
* specific cloning operation. First, if the class of this object does
-
* not implement the interface {@code Cloneable}, then a
-
* {@code CloneNotSupportedException} is thrown. Note that all arrays
-
* are considered to implement the interface {@code Cloneable} and that
-
* the return type of the {@code clone} method of an array type {@code T[]}
-
* is {@code T[]} where T is any reference or primitive type.
-
* Otherwise, this method creates a new instance of the class of this
-
* object and initializes all its fields with exactly the contents of
-
* the corresponding fields of this object, as if by assignment; the
-
* contents of the fields are not themselves cloned. Thus, this method
-
* performs a "shallow copy" of this object, not a "deep copy" operation.
-
*
-
* The class {@code Object} does not itself implement the interface
-
* {@code Cloneable}, so calling the {@code clone} method on an object
-
* whose class is {@code Object} will result in throwing an
-
* exception at run time.
-
*
-
* @return a clone of this instance.
-
* @exception CloneNotSupportedException if the object's class does not
-
* support the {@code Cloneable} interface. Subclasses
-
* that override the {@code clone} method can also
-
* throw this exception to indicate that an instance cannot
-
* be cloned.
-
* @see java.lang.Cloneable
-
*/
-
从上面我们可以看出来,比较的是两个对象的地址值,更全面的介绍,自己看下上面的 注释。
下面看下toString()方法,当然和本篇没有多少关系。
-
public String toString() {
-
return getClass().getName() + "@" + Integer.toHexString(hashCode());
-
}
-
-
返回的是包名.类名@十六进制的哈希值

下面看一小程序:

上面结果也许你会问,不都是字符串么!为什么结果不一样呢!?这个要结合java内存来分析,对于new出来的对象,是在堆中分配内存空间,而对于String类很特殊,
如果对于直接String ss = xxxx,是在常量池中分配内存空间,并不是在栈中分配。但是对于上述的a,b在栈中分配空间,指向了堆中不同的内存空间,当然两者不等!
再看一个小程序:

咦,equals()不是比较的是地址么?还是true,不能有这种定向思维,应该查阅API或者看下源码:
-
*/
-
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;
-
}
上面很清楚,String类重写了Object中的toString()方法,如果不是指向同一对象的话,那就比较内容是不是一样,所以上文中a.equals(b),b.equals(a)返回的是true.
在继承父类时,重写某个方法很重要。上文中没有说Object类中一个也很重要的方法hashcode(),这也是后续文章会写。文章作为本人学习总结,如有错误之处,请指正!谢谢。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29876893/viewspace-1823819/
,如需转载,请注明出处,否则将追究法律责任。