Run the following methods:
public class Test {
public static void Main (string[] args) {
String str = "ABC";
Char[] Array = {' A ', ' B ', ' C '};
String str2 = new string (array);
Use Intern () to put str2 string contents into a constant pool
STR2 = Str2.intern ();
This comparison is used to illustrate the string literal constants and we use intern to process the string after it is in the same place
System.out.println (str = = STR2);
Well, then, let's intern the hell out of here.
arraylist<string> list = new arraylist<string> ();
for (int i = 0; i < 50000000; i++) {
String temp = string.valueof (i). Intern ();
List.add (temp);
}
}
}
Results:
At Test.main (test.java:16)
The exception information tells us that PermGen is full. Oh, I know where the string constant pool is. PermGen is the so-called method area in the JVM specification.
The above excerpt from
Small example: How to explain the position of a string constant pool thanks to the original author!!!
Where is the Java constant pool stored