在字符串文字到另一个字符串的情况下重载+与使用字符串文字重载引用不同

时间:2015-06-14 08:57:02

标签: java string operator-overloading

在java中为以下代码: -

Hashcode s1 = 2062602683
Hashcode s2 = 2062602683
Hashcode s3 = 2062602683
Hashcode s4 = 76311326
Hashcode s5 = 2062602683
Hashcode s6 = 2062602683
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Indentity Hashcode s1 = 4337374
Indentity Hashcode s2 = 4337374
Indentity Hashcode s3 = 4337374
Indentity Hashcode s4 = 18019860
Indentity Hashcode s5 = 31054905
Indentity Hashcode s6 = 605645
Indentity Hashcode intern s6 = 4337374
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
s1 == s2 : - true
s1 == s3 : - true
s1 == s4 : - false
s1 == s5 : - false
s1 == s6 : - false

s2 == s3 : - true
s2 == s4 : - false
s2 == s5 : - false
s2 == s6 : - false

s3 == s4 : - false
s3 == s5 : - false
s3 == s6 : - false

s4 == s5 : - false
s4 == s6 : - false

s5 == s6 : - false

s1 == s6.intern() : - true
s2 == s6.intern() : - true
s3 == s6.intern() : - true
s4 == s6.intern() : - false
s5 == s6.intern() : - false

第一个比较回复真实,第二个比较返回flase。为什么这样?

注意: - 两者仅导致“ab”字符串

0 个答案:

没有答案