java:
String s1 = "abc";
String a1 = "a";
String b1 = "b";
String c1 = "c";
String s2 = a1+b1+c1;
s1.equals(s2)===>true
(s1==s2)===>false
c#:
string s1 = "abc";
string a1 = "a";
string b1 = "b";
string c1 = "c";
string s2 = a1+b1+c1;
s1.Equals(s2)===>True
(s1==s2)===>True
总结:
java中String equals实现的代码如下:
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;
}
java中==比较的是引用是否相同,equals先比较引用是否相同,若不同再比较值是否相同。
if (value == null)
{
// exception will be thrown later for null this
if (this != null) return false;
}
return EqualsHelper(this, value);
}
return String.Equals(a, b);
}
if ((Object)a==(Object)b) {
return true;
}
if ((Object)a==null || (Object)b==null) {
return false;
}
return EqualsHelper(a, b);
}