String对象的equals()与 = =

最近看了网上有人说,String对象的equals()与==的区别,不知道两者到底有什么区别,如果真的要彻底搞清楚,我们还是从“万类之源”说起,http://blog.itpub.net/29876893/viewspace-1819489/这是之前写关于Object的一篇文章,现在我们还是从源码入手(当然结合API更好)。
下面还是分析Object类中的equals()方法,关于源码如何获取上篇文章已经说的很清楚。

下面是Object类中的equals():

点击(此处)折叠或打开

  1. public boolean equals(Object obj) {
  2.         return (this == obj);
  3.     }


  4.     /**
  5.      * Creates and returns a copy of this object. The precise meaning
  6.      * of "copy" may depend on the class of the object. The general
  7.      * intent is that, for any object {@code x}, the expression:
  8.      *

  9.      *
    			
  10.      * x.clone() != x
  11.      * will be true, and that the expression:
  12.      *

  13.      *
    			
  14.      * x.clone().getClass() == x.getClass()
  15.      * will be {@code true}, but these are not absolute requirements.
  16.      * While it is typically the case that:
  17.      *

  18.      *
    			
  19.      * x.clone().equals(x)
  20.      * will be {@code true}, this is not an absolute requirement.
  21.      *


  22.      * By convention, the returned object should be obtained by calling
  23.      * {@code super.clone}. If a class and all of its superclasses (except
  24.      * {@code Object}) obey this convention, it will be the case that
  25.      * {@code x.clone().getClass() == x.getClass()}.
  26.      *


  27.      * By convention, the object returned by this method should be independent
  28.      * of this object (which is being cloned). To achieve this independence,
  29.      * it may be necessary to modify one or more fields of the object returned
  30.      * by {@code super.clone} before returning it. Typically, this means
  31.      * copying any mutable objects that comprise the internal "deep structure"
  32.      * of the object being cloned and replacing the references to these
  33.      * objects with references to the copies. If a class contains only
  34.      * primitive fields or references to immutable objects, then it is usually
  35.      * the case that no fields in the object returned by {@code super.clone}
  36.      * need to be modified.
  37.      *


  38.      * The method {@code clone} for class {@code Object} performs a
  39.      * specific cloning operation. First, if the class of this object does
  40.      * not implement the interface {@code Cloneable}, then a
  41.      * {@code CloneNotSupportedException} is thrown. Note that all arrays
  42.      * are considered to implement the interface {@code Cloneable} and that
  43.      * the return type of the {@code clone} method of an array type {@code T[]}
  44.      * is {@code T[]} where T is any reference or primitive type.
  45.      * Otherwise, this method creates a new instance of the class of this
  46.      * object and initializes all its fields with exactly the contents of
  47.      * the corresponding fields of this object, as if by assignment; the
  48.      * contents of the fields are not themselves cloned. Thus, this method
  49.      * performs a "shallow copy" of this object, not a "deep copy" operation.
  50.      *


  51.      * The class {@code Object} does not itself implement the interface
  52.      * {@code Cloneable}, so calling the {@code clone} method on an object
  53.      * whose class is {@code Object} will result in throwing an
  54.      * exception at run time.
  55.      *
  56.      * @return a clone of this instance.
  57.      * @exception CloneNotSupportedException if the object's class does not
  58.      * support the {@code Cloneable} interface. Subclasses
  59.      * that override the {@code clone} method can also
  60.      * throw this exception to indicate that an instance cannot
  61.      * be cloned.
  62.      * @see java.lang.Cloneable
  63.      */
从上面我们可以看出来,比较的是两个对象的地址值,更全面的介绍,自己看下上面的 注释。

下面看下toString()方法,当然和本篇没有多少关系。

点击(此处)折叠或打开

  1. public String toString() {
  2.         return getClass().getName() + "@" + Integer.toHexString(hashCode());
  3.     }

  4.     
返回的是包名.类名@十六进制的哈希值




下面看一小程序:

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

再看一个小程序:



咦,equals()不是比较的是地址么?还是true,不能有这种定向思维,应该查阅API或者看下源码:

点击(此处)折叠或打开

  1. */
  2.     public boolean equals(Object anObject) {
  3.         if (this == anObject) {
  4.             return true;
  5.         }
  6.         if (anObject instanceof String) {
  7.             String anotherString = (String) anObject;
  8.             int n = value.length;
  9.             if (n == anotherString.value.length) {
  10.                 char v1[] = value;
  11.                 char v2[] = anotherString.value;
  12.                 int i = 0;
  13.                 while (n-- != 0) {
  14.                     if (v1[i] != v2[i])
  15.                             return false;
  16.                     i++;
  17.                 }
  18.                 return true;
  19.             }
  20.         }
  21.         return false;
  22.     }
上面很清楚,String类重写了Object中的toString()方法,如果不是指向同一对象的话,那就比较内容是不是一样,所以上文中a.equals(b),b.equals(a)返回的是true.
在继承父类时,重写某个方法很重要。上文中没有说Object类中一个也很重要的方法hashcode(),这也是后续文章会写。文章作为本人学习总结,如有错误之处,请指正!谢谢。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29876893/viewspace-1823819/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29876893/viewspace-1823819/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值