java學習:字串比較“==”與“equals”的差異及與c#的區別

來源:互聯網
上載者:User

.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)); //true10             Console.WriteLine("String.Compare(s1,s,true) ? " + String.Compare(s1, s, true)); //011             Console.WriteLine("------------------------");12 13 14             char[] chr = { 'a', 'b', 'c' };15             String s3 = new String(chr);16             Console.WriteLine("s1==s3 ? " + (s1 == s3)); //true17             Console.WriteLine("s1.equals(s3) ? " + s1.Equals(s3)); //true18             Console.WriteLine("String.Compare(s3, s, true) ? " + String.Compare(s3, s, true)); //019             Console.WriteLine("------------------------");20 21             String t = "bc";22             String s4 = "a" + t;23             Console.WriteLine("s1==s4 ? " + (s1 == s4)); //true24             Console.WriteLine("s1.equals(s4) ? " + s1.Equals(s4)); //true25             Console.WriteLine("String.Compare(s4, s, true) ? " + String.Compare(s4, s, true)); //026             Console.WriteLine("------------------------");27 28             String s5 = "a" + "bc";29             Console.WriteLine("s1==s5 ? " + (s1 == s5)); //true30             Console.WriteLine("s1.equals(s5) ? " + s1.Equals(s5)); //true31             Console.WriteLine("String.Compare(s5, s, true) ? " + String.Compare(s5, s, true)); //032 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)); //false13         System.out.println("s1.equals(s3) ? " + s1.equals(s3)); //true    14         System.out.println("s3.equalsIgnoreCase(s) ? " + s3.equalsIgnoreCase(s)); //true15         System.out.println("------------------------");16         17         char[] chr ={'a','b','c'};18         String s4 = new String(chr);19         System.out.println("s1==s4 ? " + (s1==s4)); //false20         System.out.println("s1.equals(s4) ? " + s1.equals(s4)); //true21         System.out.println("s4.equalsIgnoreCase(s) ? " + s4.equalsIgnoreCase(s)); //true22         System.out.println("------------------------");23         24         String t ="bc";25         String s5 = "a" + t;26         System.out.println("s1==s5 ? " + (s1==s5)); //false27         System.out.println("s1.equals(s5) ? " + s1.equals(s5)); //true28         System.out.println("s5.equalsIgnoreCase(s) ? " + s5.equalsIgnoreCase(s)); //true29         System.out.println("------------------------");30         31         String s6 = "a" + "bc";32         System.out.println("s1==s6 ? " + (s1==s6)); //true33         System.out.println("s1.equals(s6) ? " + s1.equals(s6)); //true34         System.out.println("s6.equalsIgnoreCase(s) ? " + s6.equalsIgnoreCase(s)); //true35         36         37     }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.