Java基礎複習:String類注意點

來源:互聯網
上載者:User
/* * String是不可變的類,一旦建立,包含在對象中的字元序列是不可變的 * 一個String對象的內容不能變,一旦內容變數該對象就變成一個新的String對象了 * String常量和基本類型常量都存放在JVM的一塊獨立的常量池中 * String使用private final char value[]來實現儲存 */public class StingDemo {public static void main(String[] args) {String s1 = "hello";String s2 = "hello";//s1,s2指向的的是常量池中的同一塊地區System.out.println(s1==s2);//true//equals()方法比較的是內容System.out.println(s1.equals(s2));//trueSystem.out.println("===================");String s3 = new String("well");String s4 = new String("well");//s3,s4指向堆中的不同地址,但其內容都定位於常量池中的同一塊地區System.out.println(s3==s4);//falseSystem.out.println(s3.equals(s4));//true}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.