public class StringTest {
public static String one = null;
public static String two = null;
public static String three = null;
public static void modifystr1(String str) {
System.out.println("str:"+str);
System.out.println("one:"+one);
System.out.println(one == str);
str = "string one";
System.out.println(one == str);
System.out.println();
}
public static void modifystr2(String str) {
System.out.println("str:"+str);
System.out.println("two:"+two);
System.out.println(two == str);
str = "string two";
System.out.println(two == str);
System.out.println();
}
public static void modifystr3(String str) {
System.out.println("str:"+str);
System.out.println("three:"+three);
System.out.println(three == str);
str = "string three";
System.out.println(three == str);
System.out.println();
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("start one");
one = new String("string one");
modifystr1(one);
System.out.println("start two");
two = new String("string two").intern();
modifystr2(two);
System.out.println("start three");
three = "string three";
modifystr3(three);
}
}
运行结果:
start one
str:string one
one:string one
true
false
start two
str:string two
two:string two
true
true
start three
str:string three
three:string three
true
true