java和c#语言上的一个不同之处
请看下面代码:
java:
String s1 = "abc";
String s2 = new String("abc");
String s3 = "a" + "bc";
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s1 == s2);
System.out.println(s1 == s3);
c#:
String s1 = "abc";
String s2 = new String(new char[] {'a', 'b', 'c'});
String s3 = "a" + "bc";
Console.WriteLine(s1.Equals(s2));
Console.WriteLine(s1.Equals(s3));
Console.WriteLine(s1 == s2);
Console.WriteLine(s1 == s3);
运行结果是:
java:
true
true
false
true
c#:
true
true
true
true
解释:
java中,string.equals逐一比较字符,==比较引用
c#中,我就不多说了,这里是博客园,都对这个比较熟了:)