contentEquals与equals区别?

解决方案 »

  1.   

    contentEquals(CharSequence cs) 
              当且仅当此 String 表示与指定序列相同的 char 值时,才返回 true。 
     boolean contentEquals(StringBuffer sb) 
              当且仅当此 String 表示与指定的 StringBuffer 相同的字符序列时,才返回 true。 
    equals(Object anObject) 
              比较此字符串与指定的对象。
      

  2.   

    equals:The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. equals,当参数为null的时候,返回的是false
      

  3.   

    说的很清楚。Java Api 中有详细的讲解,你可以参阅一下,最好是看英文版的
      

  4.   

      这只是JDK上讲的片面的吗??、
       有自己的理解吗?
      

  5.   

    其实这几个方法功能是一样的,只是参数类型不一样。下面是string的几个方法的源码实现:
      public boolean contentEquals(StringBuffer sb) {
            synchronized(sb) {
                return contentEquals((CharSequence)sb);
            }
        }public boolean contentEquals(CharSequence cs) {
            if (count != cs.length())
                return false;
            // Argument is a StringBuffer, StringBuilder
            if (cs instanceof AbstractStringBuilder) {
                char v1[] = value;
                char v2[] = ((AbstractStringBuilder)cs).getValue();
                int i = offset;
                int j = 0;
                int n = count;
                while (n-- != 0) {
                    if (v1[i++] != v2[j++])
                        return false;
                }
            }
            // Argument is a String
            if (cs.equals(this))
                return true;
            // Argument is a generic CharSequence
            char v1[] = value;
            int i = offset;
            int j = 0;
            int n = count;
            while (n-- != 0) {
                if (v1[i++] != cs.charAt(j++))
                    return false;
            }
            return true;
        }public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof 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;
        }希望对你有帮助