[Java] String Memory Analysis

Source: Internet
Author: User

 Public classstringtest{ Public Static voidMain (string[] args) {String str1=NewString ("123"); String str2=NewString ("123"); //str1 and str2 point to different memorySystem.out.println (STR1==STR2);//falseSystem.out.println (Str1.equals (str2));//trueString STR3= "123"; String STR4= "123"; System.out.println (STR3= = STR4);//STR3 and Str4 point to the same piece of memory trueSystem.out.println (Str3.equals (STR4));//trueSTR3= "234"; STR4= "456"; STR4= "789"; String STR5= "456"; //A string constant that, whenever declared, has a memory allocation, even if it is not pointed to and is not garbage collected, STR5 no longer allocates memory because it already exists//in "456" This block of memory is directed directly.                 /*** Because string is a string constant, a chunk of memory is allocated for each new string, and the following loop allocates 100 blocks of memory, but only the last piece is used, and the memory in the string pool is not recycled, so if you do a lot of string overlays, Do not * Use this method*/String STR6= "";  for(inti=0;i<100;i++) {STR6+=i;    } System.out.println (STR6); }}

[Java] String Memory Analysis

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.