Java Some validation of assignment symbols for string classes

Source: Internet
Author: User

As we all know, many Java books say that the contents of a string object cannot be modified, but in practice the assignment notation can still be used in the created String class object, as shown in the following code:

1String S1 = "ABS";2String s2 = "ABS";3System.out.println ("Before Change:");4System.out.println ("S1 is" +s1);5System.out.println ("S2 is" +S2);6System.out.println ("S1 = = S2 is" + (S1 = =s2));7S1 = S1.replace (' A ', ' C ');8System.out.println ("After Change:");9System.out.println ("S1 is" +s1);TenSystem.out.println ("S2 is" +S2); OneSystem.out.println ("S1 = = S2 is" + (S1 = = s2));

The results of the program running are as follows:

As can be seen from the results:

S1 can be modified with an assignment symbol, and no error will be given at this time. At first, S1 and S2 point to the same address. When S1 is modified,s2 's content does not change,s1 and S2 will no longer point to the same address. At this point,S1 should have re-created an object.

Here to point out is the statement s1.replace (' A ', ' C '); Returns a new string and does not change the contents of the original string. If you do not simply run S1.replace (' A ', ' C '), the contents of S1 will not change.

If you connect a string class object that you have created with an assignment symbol to another string class object, the effect will be different, as shown in the code:

1String S1 = "ABS";2 3String s2 = "ABS";4 5String s3 =NewString ("ABS");6 7System.out.println ("Before Change:");8 9System.out.println ("S1 is" +s1);Ten  OneSystem.out.println ("S2 is" +S2); A  -System.out.println ("S3 is" +S3); -  theSystem.out.println ("S1 = = S2 is" + (S1 = =s2)); -  -System.out.println ("S2 = = S3 is" + (S2 = =S3)); -  +S3 =S1; -  +System.out.println ("After Change:"); A  atSystem.out.println ("S1 is" +s1); -  -System.out.println ("S2 is" +S2); -  -System.out.println ("S3 is" +S3); -  inSystem.out.println ("S1 = = S2 is" + (S1 = =s2)); -  toSystem.out.println ("S2 = = S3 is" + (S2 = = s3));

The results of the code run are as follows:

As you can see, the original S1 and S3 do not point to the same location, But when s3 = S1 is executed, S3 points to the S1 address, at which point s2,s3 points to the same address. According to the Java recycling mechanism, the object of the address to which the original S3 is pointing should be recycled.

Java Some validation of assignment symbols for string classes

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.