String用法详解(equal源码 ==和equal的解释、字面赋值和new赋值效率、重写了hashcode的方法解释)

    技术2022-05-20  31

    String  a = “abc”;//在字符串池中找abc,如果有,就直接返回地址,如果没有就加值abc然后再返回地址(此方式的值是存放在字符串池中)

    String  b =  “abc”;

    String  c  =   new String("abc");//在字符串池中找abc,如果有,就不管,如果没有就在对象池中加个abc,然后在堆中建一个对象abc,返回地址(此方式值存放在堆中的)

    String  d  =   new String("abc");

     

    a ==b    true

    a.equal(b)   true

     

    b==c     false;

    b.equal(c)   true;

     

    c==d  false;

    c.equal(d)  true;

     

    equals方法说明

    如果是String对象就调用string的equals,就比较他的值

    如果是其他对象,默认调用Object的equals,比较是不是同一个引用和==相同,

    如果是其他对象,而且自己实现了equals方式,就按照他自己的equals对象的比较方式。

     

     

     

    Object equal方法的源代码

    public boolean equals(Object obj) {

             return (this == obj);// 判断调用对象和传入对象的地址是不是一样的

    }

     

     

    Stirng equal 方法的源代码

    public boolean equals(Object anObject) {

             // 自己跟自己比

    if (this == anObject) {

                 return true;

             }

             if (anObject instanceof String) {// 判断是 String 的实例

                 String anotherString = (String)anObject;// 强制转化 , 以防父类用子类的方法用不到

                 // 一个字符一个字符的比较

                 int n = count;

                 if (n == anotherString.count) {

                       char v1[] = value;

                       char v2[] = anotherString.value;

                       int i = offset;

                       int j = anotherString.offset;

                       while (n-- != 0) {

                           if (v1[i++] != v2[j++])

                                return false;

                       }

                       return true;

                 }

             }

             return false;

         }

     

     

    java String 的字面赋值方式 public String intern() Returns a canonical (标准)representation for the string object. A pool of strings, initially empty, is maintained(维护) privately by the class String. When the intern method is invoked, if the pool already contains a string equal to this String object as determined(决定) by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned. It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true. All literal(字面) strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification Returns: a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings. Lexical Structure >Literals >String Literals The Java Language Specification (所有虚拟机都必须要实现的东西) 字符串池里面是不会丢的, 而堆里面是会被回收的,使用字面建string效率会高. string的hashcode的源码 /** * Returns a hash code for this string. The hash code for a * <code>String</code> object is computed as * <blockquote><pre> * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] * </pre></blockquote> * using <code>int</code> arithmetic, where <code>s[i]</code> is the * <i>i</i>th character of the string, <code>n</code> is the length of * the string, and <code>^</code> indicates exponentiation. * (The hash value of the empty string is zero.) * * @return a hash code value for this object. */ //就是按s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]公式来生成int序列 public int hashCode() { int h = hash; if (h == 0) { int off = offset; char val[] = value; int len = count; for (int i = 0; i < len; i++) { h = 31*h + val[off++]; } hash = h; } return h; }  

    最新回复(0)