import java.lang.ref.WeakReference;
/**
* Class to research strings.
* constant string like "AAA" won't be reclaimed while instance created by new will.
*/
public class StringResearch { public static void main(String[] args) {
String a = "AAA";
String b = new String("AAA"); WeakReference wra = new WeakReference(a);
WeakReference wrb = new WeakReference(b);
a = null;
b = null; System.gc();
System.out.println(wra.get());
System.out.println(wrb.get());
}}程序输出如下
AAA
null根据以上结果,请说明通常情况下如何定义新的String变量比较好,并说明理由。
/**
* Class to research strings.
* constant string like "AAA" won't be reclaimed while instance created by new will.
*/
public class StringResearch { public static void main(String[] args) {
String a = "AAA";
String b = new String("AAA"); WeakReference wra = new WeakReference(a);
WeakReference wrb = new WeakReference(b);
a = null;
b = null; System.gc();
System.out.println(wra.get());
System.out.println(wrb.get());
}}程序输出如下
AAA
null根据以上结果,请说明通常情况下如何定义新的String变量比较好,并说明理由。
不知道是不是这个意思