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