论坛首页 Java企业应用论坛

Java源代码阅读体会(1)-string

浏览 6331 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (7)
作者 正文
   发表时间:2012-03-09  

今天我看到String的源代码,有点迷糊,还请大家不吝赐教。

 

/**
     * 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 = 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;
    }
 

这个是String中比较值是否相等的代码,我不明白第一个分支this == anObject,这个在什么时候会成立呢?

   发表时间:2012-03-09  
ludatong110 写道

今天我看到String的源代码,有点迷糊,还请大家不吝赐教。

 

/**
     * 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 = 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;
    }
 

这个是String中比较值是否相等的代码,我不明白第一个分支this == anObject,这个在什么时候会成立呢?

 

 

自反性。

 

建议楼主去看看JDK 文档

 

 写道
equals
public boolean equals(Object obj)指示其他某个对象是否与此对象“相等”。
equals 方法在非空对象引用上实现相等关系:

自反性:对于任何非空引用值 x,x.equals(x) 都应返回 true。
对称性:对于任何非空引用值 x 和 y,当且仅当 y.equals(x) 返回 true 时,x.equals(y) 才应返回 true。
传递性:对于任何非空引用值 x、y 和 z,如果 x.equals(y) 返回 true,并且 y.equals(z) 返回 true,那么 x.equals(z) 应返回 true。
一致性:对于任何非空引用值 x 和 y,多次调用 x.equals(y) 始终返回 true 或始终返回 false,前提是对象上 equals 比较中所用的信息没有被修改。
对于任何非空引用值 x,x.equals(null) 都应返回 false。
Object 类的 equals 方法实现对象上差别可能性最大的相等关系;即,对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象时,此方法才返回 true(x == y 具有值 true)。

注意:当此方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。


参数:
obj - 要与之比较的引用对象。
返回:
如果此对象与 obj 参数相同,则返回 true;否则返回 false。
另请参见:
hashCode(), Hashtable
 

 

 

0 请登录后投票
   发表时间:2012-03-09  
对象和自己比较的时候,a.equals(a),就会走到这个分支,表示是同一个对象引用。
0 请登录后投票
   发表时间:2012-03-09  
==比较的是地址,当两个参数地址相同时返回true,那两个参数地址会相同呢,就是自己和自己比较时
0 请登录后投票
   发表时间:2012-03-09   最后修改:2012-03-09
我查看了一下,是在String的contentEquals方法比较时会调用这个方法,的确是同值引用了。子类以父类方式出现比较,String父接口CharSequence。
0 请登录后投票
   发表时间:2012-03-11  
ITeye沦陷了,这都上首页了
0 请登录后投票
   发表时间:2012-03-12  
但是这个东西俺真的不会。。知识虽小,那也是受教了。。
0 请登录后投票
   发表时间:2012-03-12  
当你进行 this.equals(this)比较的时候,这句话就成立了。
0 请登录后投票
   发表时间:2012-03-12  
没看到什么体会,就看到是来问问题的

把问题封装成体会,哎

这个相等是对象本身相等  栈信息存储的指针都是指的相同的地址
0 请登录后投票
   发表时间:2012-03-12  
JVM先拿到2个对象的地址进行比较,如果两个对象引用都是一样,那他们的值能不一样吗?那么就没有必要进行值比较了。
如果引用不一样的话,才开始比较他们的值是否一样。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics