看源码,加深理解---(零)String#equals(Object)

来源:互联网 发布:c 遍历二维数组 编辑:程序博客网 时间:2024/05/20 06:31

零,写在最前面

看源码的目的在于通过源码了解java是如何工作的,有哪些地方可以借鉴

一,首先附上源码

/**     * Compares this string to the specified object.  The result is {@code     * true} if and only if the argument is not {@code null} and is a {@code     * String} object that represents the same sequence of characters as this     * object.     *     * @param  anObject     *         The object to compare this {@code String} against     *     * @return  {@code true} if the given object represents a {@code String}     *          equivalent to this string, {@code false} otherwise     *     * @see  #compareTo(String)     * @see  #equalsIgnoreCase(String)     */    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;    }

二,然后看一下源码上面的注释

/**java     * Compares this string to the specified object.  The result is {@code     * true} if and only if the argument is not {@code null} and is a {@code     * String} object that represents the same sequence of characters as this     * object.     *     * @param  anObject     *         The object to compare this {@code String} against     *     * @return  {@code true} if the given object represents a {@code String}     *          equivalent to this string, {@code false} otherwise     *     * @see  #compareTo(String)     * @see  #equalsIgnoreCase(String)     */

注释的内容如下:
       将此字符串和指定对象进行比较,结果为真的的情况有且只有参数不为空且为字符串的对象并且与此相同字符序列的对象.
简单的说就是:
       此字符串和指定对象进行比较,只有这个对象不为空,是字符串并且具有与此字符串相同的字符串序列才会返回真.

三,看一下源码

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;    }

解释如下:
       首先,方法的返回值为布尔类型,获取的参数为Object类型.方法的第一步,首先比较字符串和对象的地址是否相同,相同的话直接返回真,如果不是同一个对象,判断对象是不是String类的子类,如果是,将对象的类型强转为String,并赋值给anotherString引用.定义一个整数型的n来存储value的长度,这里涉及到一个成员变量/** The value is used for character storage. */private final char value[];`即该值用于字符存储.然后如果该字符串与指定对象的长度相同,则进入if判断中,创建一个char类型的数组v1来保存字符串的结果,创建一个char类型的数组v2来保存指定对象的结果.定义一个int姓的对象i赋初值为1.用while循环遍历v1和v2的值,对比v1和v2中相同位置的字节,只要有一处不能对应则返回假.

原创粉丝点击