Java Some validation of assignment symbols for string classes

Source: Internet
Author: User
Tags code chart

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:

650) this.width=650; "style=" border:medium none; "alt=" Copy Code "src=" Http://common.cnblogs.com/images/copycode.gif "/ >

 1     String s1 =  "ABS"; 2      string s2 =  "ABS";  3     system.out.println ("Before change:" );  4     system.out.println ("s1 is " +s1); 5      system.out.println ("s2 is " +s2);  6     system.out.println ( "s1 == s2 is "  +  (S1 == S2));  7     s1  = s1.replace (' A ', ' C ');  8     system.out.println ("After change:") ;  9     system.out.println ("s1 is " +s1);10      system.out.println ("s2 is " +s2); 11     system.out.println ("s1 ==  s2 is  " +  (S1 == S2));

650) this.width=650; "style=" border:medium none; "alt=" Copy Code "src=" Http://common.cnblogs.com/images/copycode.gif "/ >

The results of the program running are as follows:

650) this.width=650; "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; "src="/HTTP/ Images2015.cnblogs.com/blog/938222/201604/938222-20160420194043898-1078337582.png "/>

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:

65 0) this.width=650; "style=" border:medium none; "alt=" Copy Code "src=" Http://common.cnblogs.com/images/copycode.gif "/>

 1 String s1 =  "ABS"; 2  3 string s2 =  "ABS";  4  5 String s3 = new String  ("abs"); 6  7  System.out.println ("Before change:");  8  9 system.out.println ("s1 is " +s1); 10 11 system.out.println ("s2 is " +s2); 12 13 system.out.println ("s3 is  "+s3"); 14 15 system.out.println ("s1 == s2 is "  +  (S1 == S2)); 16 17 system.out.println ("s2 == s3 is "  +  (S2 == S3)); 18  19 s3 = s1;20 21 system.out.println ("After change:");22 23  System.out.println ("s1 is " +s1); 24 25 system.out.println ("s2 is " +s2); 26 27  system.out.println ("s3 is " +S3); 28 29 system.out.println ("S1 == s2 is   " +  (S1 == S2) 30 31 system.out.println ("s2 == s3 is "  +  (s2 ==  S3));

650) this.width=650; "style=" border:medium none; "alt=" Copy Code "src=" Http://common.cnblogs.com/images/copycode.gif "/ >

The results of the code run are as follows:

650) this.width=650; "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; "src="/HTTP/ Images2015.cnblogs.com/blog/938222/201604/938222-20160420194810757-1341010539.png "/>

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.

get ""  

Performance monitoring: Monitoring the performance of the entire system, SQL monitoring, SQL firewall, URL monitoring, spring monitoring, session monitoring, etc.
11. Interface Test: POST or GET mode detection system interface, parameter encryption, JSON return results, calculate server response time
12. Send mail: single, Mass mailing
13. QR Code: Generate two-dimensional code chart save to server or parse to read the information in two-dimensional code
14. Chart Report: Bar chart, pie chart, line chart, various chart Daquan
15. Map Tool: Open map, mouse click on the map to get latitude and longitude coordinates, calculate two points distance according to latitude and longitude
Print test: Page Print preview Test
Image management: Unified management of batch uploaded images, click to enlarge, can open multiple, free switch, brilliant preview effect
Picture crawler: Enter a URL, crawl out of its picture displayed on the page, you can enlarge the preview. Can be saved to the server, to the picture management inside
19. Inside Letter: Receive the mailbox and send the mailbox, websocket the technical communication technology to do the prompt receipt reminder, may configure the voice prompt letter



Java Some validation of assignment symbols for string classes

Related Article

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.