美文网首页
String#equals()和String#contentEq

String#equals()和String#contentEq

作者: BlackNeko | 来源:发表于2016-10-09 23:29 被阅读1532次

    String#equals()不仅比较这个字符串的内容还检查另一个被比较的对象是否是String类型,String#contentEquals()只比较两者的内容是否相同,不检查被比较对象的类型。

    String s = "hello world";
    StringBuilder sb = new StringBuilder("hello world");
    
    System.out.println(s.equals(sb));
    System.out.println(s.contentEquals(sb));
    

    output:

    false
    true
    

    String#equals()方法:

    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        //如果不是String继承体系的类相比较,都返回false
        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#contentEquals()源码,String#contentEquals(StringBuffer sb)String#contentEquals(CharSequence cs)一样的:

    public boolean contentEquals(CharSequence cs) {
        // Argument is a StringBuffer, StringBuilder
        if (cs instanceof AbstractStringBuilder) {
            if (cs instanceof StringBuffer) {
                synchronized(cs) {
                   return nonSyncContentEquals((AbstractStringBuilder)cs);
                }
            } else {
                return nonSyncContentEquals((AbstractStringBuilder)cs);
            }
        }
        // Argument is a String
        if (cs instanceof String) {
            return equals(cs);
        }
        // Argument is a generic CharSequence
        char v1[] = value;
        int n = v1.length;
        if (n != cs.length()) {
            return false;
        }
        for (int i = 0; i < n; i++) {
            if (v1[i] != cs.charAt(i)) {
                return false;
            }
        }
        return true;
    }
    

    再看nonSyncContentEquals()方法:

    private boolean nonSyncContentEquals(AbstractStringBuilder sb) {
        char v1[] = value;
        char v2[] = sb.getValue();
        int n = v1.length;
        if (n != sb.length()) {
            return false;
        }
        for (int i = 0; i < n; i++) {
            if (v1[i] != v2[i]) {
                return false;
            }
        }
        return true;
    }
    

    比较String类型与String,StringBuilder, StringBuffer, CharBuffer,等类型内容的相等性使用String#contentEquals()

    参考:

    Difference between String#equals and String#contentEquals methods

    相关文章

      网友评论

          本文标题:String#equals()和String#contentEq

          本文链接:https://www.haomeiwen.com/subject/kqymyttx.html