黑马程序员技术交流社区
标题:
在String和StringBuilder使用中出现了疑惑???
[打印本页]
作者:
莞漂族
时间:
2014-6-21 12:24
标题:
在String和StringBuilder使用中出现了疑惑???
问题:1、为什么在
method_1方法中
s1 = s2;执行后在该方法中打印s1的结果和在main方法中打印s1的结果不一样?
2、为什么在
method_2方法中
s11.append(s22);执行后,在
main方法中打印s11的结果变为javahello了而不是java?
class StringTest
{
public static void main(String[] args)
{
String s1 = "java";
String s2 = "hello";
method_1(s1,s2);
System.out.println(s1+"...."+s2); //java....hello
StringBuilder s11 = new StringBuilder("java");
StringBuilder s22 = new StringBuilder("hello");
method_2(s11,s22);
System.out.println(s11+"-----"+s22); //javahello-----hello
}
public static void method_1(String s1,String s2)
{
s1.replace('a','k');
s1 = s2;
System.out.println(s1); //hello
}
public static void method_2(StringBuilder s11,StringBuilder s22)
{
s11.append(s22);
s11 = s22;
System.out.println(s11); //hello
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2