String和常量池值的變化,string常量池

來源:互聯網
上載者:User

String和常量池值的變化,string常量池


public static void main(String[] args) { Integer i1 = new Integer(1); Integer i2 = new Integer(1); // i1,i2分別位於堆中不同的記憶體空間 System.out.println("i1 == i2:" + (i1 == i2));// 輸出false String s1 = "china"; String s2 = "china"; String ss1 = new String("china"); String ss2 = new String("china"); System.out.println("s1 == s2:" + (s1 == s2)); System.out.println("ss1 == ss2" + (ss1 == ss2)); System.out.println("s1 == ss2" + (s1 == ss2)); // 利用Java反射機制改變string的值 Field f = null; Field f1 = null; try { f = s1.getClass().getDeclaredField("value"); f1 = ss1.getClass().getDeclaredField("value"); } catch (NoSuchFieldException e) { e.printStackTrace(); } f.setAccessible(true); f1.setAccessible(true); try { f.set(s1, new char[] { 'c', 'h', 'i', 'n', 'b' }); f1.set(ss1, new char[] { 'c', 'h', 'i', 'n', 'b' }); } catch (Exception e) { e.printStackTrace(); } System.out.println("改變後的s1的值:" + s1); System.out.println("s2的值是:" + s2); System.out.println("ss1的值是:" + ss1); System.out.println("s1 == s2:" + (s1 == s2)); System.out.println("ss1 == ss2" + (ss1 == ss2)); System.out.println("s1 == ss2" + (s1 == ss2)); System.out.println("============分割線=================="); System.out.println("改變後的ss1的值:" + ss1); System.out.println("ss2的值是:" + ss2); }

列印的結果:

i1 == i2:false
s1 == s2:true
ss1 == ss2false
s1 == ss2false
改變後的s1的值:chinb
s2的值是:chinb
ss1的值是:chinb
s1 == s2:true
ss1 == ss2false
s1 == ss2false
============分割線==================
改變後的ss1的值:chinb
ss2的值是:china

聯繫我們

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