星期四, 12月 07, 2006

Java與C#的string equal與==

根據陈叙远的世界


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