论坛首页 Java企业应用论坛

这些题你做对了几道?

浏览 17689 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2010-12-16  
mercyblitz 写道
hobitton 写道
chenyongxin 写道
mercyblitz 写道
chenyongxin 写道

1.true or false?

 

		String a = "ab";
		String b = "a" + "b";
		System.out.println(a==b);

 2.true or false?

 

		String a = "abc";
		String b="ab";
		String c=b+"c";
		System.out.println(a==c);

 

 3.true or false? 

 

		String a = "ab1";
		String b = "ab"+1;
		System.out.println(a==b);

 4.ture or false?

 

		String a = "ab1";
		String b = "ab"+1;
		String c = b + "";
		System.out.println(a==c);

 

 5.true or false?   false

 

		String a = new String("abc");
		String b = "abc";
		System.out.println(a==b);

 6.true or false?  true

 

		String a = "abc";
		String b = "abc";
		System.out.println(a==b);

 

 

 

 

楼主,问你个问题,

 

为什么String的equals方法,没有hashCode方法做判断?

 

 


因为是内容比较而不是地址比较

貌似没谁规定equals方法需要用hashcode做判断,,,,这提问提的。

 

 

我这么问是有道理的,不是规定的问题。

 

equals实现建议中,hashCode不等的话,那么直接reutrn false.

 

你也可以想想为什么?

 

 

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

    请教下,jdk错了?

0 请登录后投票
   发表时间:2010-12-16  
chenyongxin 写道
mercyblitz 写道
hobitton 写道
chenyongxin 写道
mercyblitz 写道
chenyongxin 写道

1.true or false?

 

		String a = "ab";
		String b = "a" + "b";
		System.out.println(a==b);

 2.true or false?

 

		String a = "abc";
		String b="ab";
		String c=b+"c";
		System.out.println(a==c);

 

 3.true or false? 

 

		String a = "ab1";
		String b = "ab"+1;
		System.out.println(a==b);

 4.ture or false?

 

		String a = "ab1";
		String b = "ab"+1;
		String c = b + "";
		System.out.println(a==c);

 

 5.true or false?   false

 

		String a = new String("abc");
		String b = "abc";
		System.out.println(a==b);

 6.true or false?  true

 

		String a = "abc";
		String b = "abc";
		System.out.println(a==b);

 

 

 

 

楼主,问你个问题,

 

为什么String的equals方法,没有hashCode方法做判断?

 

 


因为是内容比较而不是地址比较

貌似没谁规定equals方法需要用hashcode做判断,,,,这提问提的。

 

 

我这么问是有道理的,不是规定的问题。

 

equals实现建议中,hashCode不等的话,那么直接reutrn false.

 

你也可以想想为什么?

 

 

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

    请教下,jdk错了?

这应该只是不同的jdk的实现不一样把,ibm的jdk里面的equals比较就用了hashCode,代码如下:

public boolean equals (Object object) {
	if (object == this) return true;
	if (object instanceof String) {
		String s = (String)object;
		if (count != s.count ||
			(hashCode != s.hashCode && hashCode != 0 && s.hashCode != 0))
				return false;
		return regionMatches(0, s, 0, count);
	}
	return false;
}
 
0 请登录后投票
   发表时间:2010-12-16  
这些在笔试面试时是非常有用的,也可以帮助我们了解字符串的创建过程,谢谢分享
0 请登录后投票
   发表时间:2010-12-16  
chenyongxin 写道
proud686 写道
我记得好多公司招聘时爱搞这玩意


公司里面喜欢考的是

{
    String str = "abc";
    changeString(str);
    System.out.print(str);
}

 public void changeString(String str){
    str = "abcd";
}

 

其实也不是说就喜欢考,是因为在很多年前我们这帮写代码的经常在这种类似的问题上搞错,结果公司损失不小。

 

其实就是=号的真实涵义,以及指针和对象的区别,不过不懂的话很容易错。假如String提供一个修改其对象的方法,恐怕就更迷糊了。

0 请登录后投票
   发表时间:2010-12-16  
mercyblitz 写道

 

我这么问是有道理的,不是规定的问题。

 

equals实现建议中,hashCode不等的话,那么直接reutrn false.

 

你也可以想想为什么?

 

貌似object的equals方法注释写的不是这个意思?

 写道
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

 当equals方法被覆写了的时候,一般需要修改hashcode方法,以维持hashcode的规定:相等对象的hashcode必须相同。

 

是equals影响hashcode而不是hashcode影响equals?

0 请登录后投票
   发表时间:2010-12-16  
yunzhiyifeng 写道
chenyongxin 写道
mercyblitz 写道
hobitton 写道
chenyongxin 写道
mercyblitz 写道
chenyongxin 写道

1.true or false?

 

		String a = "ab";
		String b = "a" + "b";
		System.out.println(a==b);

 2.true or false?

 

		String a = "abc";
		String b="ab";
		String c=b+"c";
		System.out.println(a==c);

 

 3.true or false? 

 

		String a = "ab1";
		String b = "ab"+1;
		System.out.println(a==b);

 4.ture or false?

 

		String a = "ab1";
		String b = "ab"+1;
		String c = b + "";
		System.out.println(a==c);

 

 5.true or false?   false

 

		String a = new String("abc");
		String b = "abc";
		System.out.println(a==b);

 6.true or false?  true

 

		String a = "abc";
		String b = "abc";
		System.out.println(a==b);

 

 

 

 

楼主,问你个问题,

 

为什么String的equals方法,没有hashCode方法做判断?

 

 


因为是内容比较而不是地址比较

貌似没谁规定equals方法需要用hashcode做判断,,,,这提问提的。

 

 

我这么问是有道理的,不是规定的问题。

 

equals实现建议中,hashCode不等的话,那么直接reutrn false.

 

你也可以想想为什么?

 

 

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

    请教下,jdk错了?

这应该只是不同的jdk的实现不一样把,ibm的jdk里面的equals比较就用了hashCode,代码如下:

 

public boolean equals (Object object) {
	if (object == this) return true;
	if (object instanceof String) {
		String s = (String)object;
		if (count != s.count ||
			(hashCode != s.hashCode && hashCode != 0 && s.hashCode != 0))
				return false;
		return regionMatches(0, s, 0, count);
	}
	return false;
}
 

   条条大路同罗马,不和你一道的你也不能说他错了。

0 请登录后投票
   发表时间:2010-12-16  
hobitton 写道
mercyblitz 写道

 

我这么问是有道理的,不是规定的问题。

 

equals实现建议中,hashCode不等的话,那么直接reutrn false.

 

你也可以想想为什么?

 

貌似object的equals方法注释写的不是这个意思?

 

 写道
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

 当equals方法被覆写了的时候,一般需要修改hashcode方法,以维持hashcode的规定:相等对象的hashcode必须相同。

 

是equals影响hashcode而不是hashcode影响equals?


正理!

0 请登录后投票
   发表时间:2010-12-16  
yunzhiyifeng 写道
chenyongxin 写道
mercyblitz 写道
hobitton 写道
chenyongxin 写道
mercyblitz 写道
chenyongxin 写道

1.true or false?

 

		String a = "ab";
		String b = "a" + "b";
		System.out.println(a==b);

 2.true or false?

 

		String a = "abc";
		String b="ab";
		String c=b+"c";
		System.out.println(a==c);

 

 3.true or false? 

 

		String a = "ab1";
		String b = "ab"+1;
		System.out.println(a==b);

 4.ture or false?

 

		String a = "ab1";
		String b = "ab"+1;
		String c = b + "";
		System.out.println(a==c);

 

 5.true or false?   false

 

		String a = new String("abc");
		String b = "abc";
		System.out.println(a==b);

 6.true or false?  true

 

		String a = "abc";
		String b = "abc";
		System.out.println(a==b);

 

 

 

 

楼主,问你个问题,

 

为什么String的equals方法,没有hashCode方法做判断?

 

 


因为是内容比较而不是地址比较

貌似没谁规定equals方法需要用hashcode做判断,,,,这提问提的。

 

 

我这么问是有道理的,不是规定的问题。

 

equals实现建议中,hashCode不等的话,那么直接reutrn false.

 

你也可以想想为什么?

 

 

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

    请教下,jdk错了?

这应该只是不同的jdk的实现不一样把,ibm的jdk里面的equals比较就用了hashCode,代码如下:

 

public boolean equals (Object object) {
	if (object == this) return true;
	if (object instanceof String) {
		String s = (String)object;
		if (count != s.count ||
			(hashCode != s.hashCode && hashCode != 0 && s.hashCode != 0))
				return false;
		return regionMatches(0, s, 0, count);
	}
	return false;
}
 

兄弟,IBM的JDK源码哪儿有?貌似之前整死找不到。。。

0 请登录后投票
   发表时间:2010-12-16   最后修改:2010-12-16
chenyongxin 写道
hobitton 写道
mercyblitz 写道

 

我这么问是有道理的,不是规定的问题。

 

equals实现建议中,hashCode不等的话,那么直接reutrn false.

 

你也可以想想为什么?

 

貌似object的equals方法注释写的不是这个意思?

 

 写道
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

 当equals方法被覆写了的时候,一般需要修改hashcode方法,以维持hashcode的规定:相等对象的hashcode必须相同。

 

是equals影响hashcode而不是hashcode影响equals?


正理!

 


扯到另外一个话题去了,

 

你没有明白我的意思,我说的是equals相等话,hashCode必须相等,相反,如果hashCode不等的话,equals一定不等。

 

。。。

 

因为SUN JDK中String的hashCode的实现不靠谱,hashCode可能会出现重复。

 

所以equals里面没有对比hashCode,这是有道理的。

 

至于IBM的,也没有错,因为hashCode不等的话,在逻辑上证明不是一个对象。

 

再次强调一次,equals的比较时,可以通过比较hashCode不等来判断equals的相等性,hashCode相等不能说明问题!

 

0 请登录后投票
   发表时间:2010-12-16  
nighthawk 写道
hobitton 写道
lz的帖子发的不错其实,只是没有突出重点在哪儿,如果只是出这么些题目,确实让人比较反感,如果能够把原理讲下,那么后面就没这么多人鄙视你了。第一页回帖的兄弟已经帮忙讲了原理了:)

就是,如果原理讲的深入一点,就是篇精华帖了

内牛满面啊,记得一个hr说过,管理销售人员容易因为他们只知道卖产品很好管理,但是技术人员很难管,他们脑袋转的快,不容易沟通。我在想是搞技术的没有销售那样能够用3句话就表明自己的观点还是搞技术的喜欢悟道。不的不承认hr的犀利。
0 请登录后投票
论坛首页 Java企业应用版

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