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
字符串操作与Java内存模型

本文探讨了Java中字符串操作的细节,特别是如何修改不同作用域内的字符串变量,并通过实例展示了字符串引用和内存分配的过程。

被折叠的 条评论
为什么被折叠?



