我知道字符串池是如何工作的。那说:
void doSomething1(){
System.out.println("This string is now added to the string pool and will stay there even after this method returns");
}
void doSomething2(){
String msg = new String("This string is now added to the string pool and will stay there even after this method returns");
System.out.println(msg);
}
doSomething2
优于doSomething1
. _ 考虑到某些字符串存在的可能性非常低,是否应该鼓励这样做reused
。我看到的问题是在第 1 种情况下,即使不再使用该字符串,它也会在很长一段时间内保持在范围内。