java学习:字符串比较“==”与“equals”的差异及与c#的区别

  1. 云栖社区>
  2. 博客>
  3. 正文

java学习:字符串比较“==”与“equals”的差异及与c#的区别

杨俊明 2012-12-10 22:06:00 浏览364
展开阅读全文

.net中,其字符串特有的驻留机制,保证了在同一进程中,相同字符序列的字符串,只有一个实例,这样能避免相同内容的字符串重复实例化,以减少性能开销。

先来回顾一下c#中的代码:

View Code
 1         public static void testString()
 2         {
 3             String s = "Abc";
 4             String s1 = "abc";
 5             String s2 = "abc";
 6 
 7 
 8             Console.WriteLine("s1==s2 ? " + (s1 == s2)); //true
 9             Console.WriteLine("s1.Equals(s2) ? " + s1.Equals(s2)); //true
10             Console.WriteLine("String.Compare(s1,s,true) ? " + String.Compare(s1, s, true)); //0
11             Console.WriteLine("------------------------");
12 
13 
14             char[] chr = { 'a', 'b', 'c' };
15             String s3 = new String(chr);
16             Console.WriteLine("s1==s3 ? " + (s1 == s3)); //true
17             Console.WriteLine("s1.equals(s3) ? " + s1.Equals(s3)); //true
18             Console.WriteLine("String.Compare(s3, s, true) ? " + String.Compare(s3, s, true)); //0
19             Console.WriteLine("------------------------");
20 
21             String t = "bc";
22             String s4 = "a" + t;
23             Console.WriteLine("s1==s4 ? " + (s1 == s4)); //true
24             Console.WriteLine("s1.equals(s4) ? " + s1.Equals(s4)); //true
25             Console.WriteLine("String.Compare(s4, s, true) ? " + String.Compare(s4, s, true)); //0
26             Console.WriteLine("------------------------");
27 
28             String s5 = "a" + "bc";
29             Console.WriteLine("s1==s5 ? " + (s1 == s5)); //true
30             Console.WriteLine("s1.equals(s5) ? " + s1.Equals(s5)); //true
31             Console.WriteLine("String.Compare(s5, s, true) ? " + String.Compare(s5, s, true)); //0
32 
33 
34             Console.Read();
35 
36 
37         }

从运行结果可以看出,无论你怎么折腾,只要二个字符串的内容完全相同,引用始终只有一个。

 

java中其实也有类似的机制,称为“字符串常量池”,但是java中却允许 用new String(String str)的方式创建多个相同内容的实例。为了能区别这二种情况,java中的==与equals用来判断字符串是否相等时,赋予了不同的含义。

==用于判定二个字符串是否引用相同,而equals用于判断二个字符串是否内容相同

View Code
 1 public static void testString(){
 2         String s = "Abc";
 3         String s1 = "abc";
 4         String s2 = "abc";
 5         
 6         System.out.println("s1==s2 ? " + (s1==s2)); //true
 7         System.out.println("s1.equals(s2) ? " + s1.equals(s2)); //true
 8         System.out.println("s1.equalsIgnoreCase(s) ? " + s1.equalsIgnoreCase(s)); //true
 9         System.out.println("------------------------");
10         
11         String s3 = new String("abc"); 
12         System.out.println("s1==s3 ? " + (s1==s3)); //false
13         System.out.println("s1.equals(s3) ? " + s1.equals(s3)); //true    
14         System.out.println("s3.equalsIgnoreCase(s) ? " + s3.equalsIgnoreCase(s)); //true
15         System.out.println("------------------------");
16         
17         char[] chr ={'a','b','c'};
18         String s4 = new String(chr);
19         System.out.println("s1==s4 ? " + (s1==s4)); //false
20         System.out.println("s1.equals(s4) ? " + s1.equals(s4)); //true
21         System.out.println("s4.equalsIgnoreCase(s) ? " + s4.equalsIgnoreCase(s)); //true
22         System.out.println("------------------------");
23         
24         String t ="bc";
25         String s5 = "a" + t;
26         System.out.println("s1==s5 ? " + (s1==s5)); //false
27         System.out.println("s1.equals(s5) ? " + s1.equals(s5)); //true
28         System.out.println("s5.equalsIgnoreCase(s) ? " + s5.equalsIgnoreCase(s)); //true
29         System.out.println("------------------------");
30         
31         String s6 = "a" + "bc";
32         System.out.println("s1==s6 ? " + (s1==s6)); //true
33         System.out.println("s1.equals(s6) ? " + s1.equals(s6)); //true
34         System.out.println("s6.equalsIgnoreCase(s) ? " + s6.equalsIgnoreCase(s)); //true
35         
36         
37     }

网友评论

登录后评论
0/500
评论
杨俊明
+ 关注