Let's first look at a simple program.
Public class t {private string STR = "########### java2000 ################### ##"; public void test () {string S = "@ java2000 @@@@@@@@@@@@@"; string Ss = "########### java2000 #####################"; // same as STR specially }}
Let's take a look at what this class actually generates. Let's open T. Class
The two strings we used already exist. Note that there are two strings, and the repeated strings do not appear twice.
Conclusion:
- This type of string is generated during compilation. It can be understood that when the class is loaded, it already exists in the memory and no new
- Therefore, "XYZ" in new string ("XYZ") exists during loading. The system allocates memory space based on this template and creates a new object.
- As for string STR = new string ("XYZ"); the STR can only be regarded as a reference and a variable. Therefore, only one object is created for this action. Because the "XYZ" already exists before this statement is executed. This statement cannot be calculated.
In short, this interview question mainly aims at understanding the question, and some concepts are difficult to clarify. Although we all know, they have different opinions.