String,
First, string has the concept of a literal literal constant , which is determined at compile time, and is stored directly in Chang when the class is loaded (note that a constant pool is a constant pool of classes , isolated between classes and classes).
The string that is generated at run time is not in the constant pool. If you want to put in a constant pool, you can use the intern () method.
Second, like a literal string, because it is stored in a constant pool and referenced by a constant pool, it cannot be GC.
Other knowledge:
In general, strings are immutable (imutable), so thread-safe.
However, you can modify the string by means of reflection !!! Because the underlying is char[], it can be forcibly modified after getting it.
/*** Modify string!!! * @throwsnosuchfieldexception *@throwsillegalaccessexception*/@Test Public voidRun6 ()throwsnosuchfieldexception, illegalaccessexception {String S1= "ABCDEFG"; System.out.println ("Pre-modification: s1=" +S1); Field value= String.class. Getdeclaredfield ("value"); //value is a string internal field:private final char value[]; Value.setaccessible (true); Char[] cs = (Char[]) value.get (S1); //cs = "ent". ToCharArray ();//This is a reference change, but the inside of the string is unchanged (private final char value[];)Cs[0] = ' A '; System.out.println ("Modified: s1=" +s1);}
It is important to note that you cannot modify the reference, but modify the array element directly!!!
Java String Learning