Changes in the String and constant pool values.

Source: Internet
Author: User

Changes in the String and constant pool values.


Public static void main (String [] args) {Integer i1 = new Integer (1); Integer i2 = new Integer (1); // i1, i2 is located in different memory spaces in the heap System. out. println ("i1 = i2:" + (i1 = i2); // outputs 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); // use the Java reflection mechanism to change the string value 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 value after change:" + s1); System. out. println ("s2 value:" + s2); System. out. println ("the value of ss1 is:" + 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 ("================ split line ============================"); System. out. println ("value of the changed ss1:" + ss1); System. out. println ("the value of ss2 is:" + ss2 );}

Printed result:

I1 = i2: false
S1 = s2: true
Ss1 = ss2false
S1 = ss2false
Value of s1 after change: chinb
The value of s2 is chinb.
The value of ss1 is chinb.
S1 = s2: true
Ss1 = ss2false
S1 = ss2false
==============================
Value of the changed ss1: chinb
The value of ss2 is china.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.