public class Buffer
{ public void swap(StringBuffer a, StringBuffer b)
{
a.append(" more");
b = a;
}
public static void main(String[] args)
{
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
Buffer buffer = new Buffer();
buffer.swap(a, b);
System.out.println(a+" \n"+b);
}
}
输出是
One more
Two
问:b应该也是One more,b已经指向a了啊?不解,求教!
{ public void swap(StringBuffer a, StringBuffer b)
{
a.append(" more");
b = a;
}
public static void main(String[] args)
{
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
Buffer buffer = new Buffer();
buffer.swap(a, b);
System.out.println(a+" \n"+b);
}
}
输出是
One more
Two
问:b应该也是One more,b已经指向a了啊?不解,求教!
被改变的是局部变量b的指向public void swap(StringBuffer a,StringBuffer b){
a.append(" more");
b = a;
System.out.println(" swap b :"+b);
}
One
Two
只是一个局部的!
swap(StringBuffer a,StringBuffer b)
传过来的是又建的,执行完之后释放!
{
public void swap(StringBuffer a, StringBuffer b)
{
a.append(" more");
b = a;
}
public static void main(String[] args)
{
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
Buffer buffer = new Buffer();
buffer.swap(a, b);
System.out.println(a+" \n"+b);
}
}
被改变去指向"One more"的是方法中的形参b,而方法外部的实参b仍然指向"Two",因此b最后的打印结果为:Two
{ public void swap(StringBuffer a, StringBuffer b)
{
a.append(" more");
b = a;
System.out.println("test local b:"+b);
}
public static void main(String[] args)
{
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
Buffer buffer = new Buffer();
buffer.swap(a, b);
System.out.println(a);
System.out.println(b);}
}
执行结果为:test local b:One more
One more
Two
结论:1、Stirngbuffer是对象,所以不是值传递问题 2test local b:One more 说明b的值是被改变的,只是在局部,该局部块执行结束后就被释放了!
a.append(" more"); //a b 都是栈内存的变量"One" "Two"是堆内存的内容
b = a;
return b;
} public static void main(String[] args) {
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
Buffer buffer = new Buffer();
b=buffer.swap(a, b);
System.out.println(a + " \n" + b);
}
}
建议楼主把栈内存和堆内存、参数传递部分看一下。
11楼画的那个图十分的准确。
StringBuffer b = new StringBuffer("Two"); ,都是指向heap上的对象
swap方法中的a,b只是两个StringBuffer引用的拷贝,但是这两个拷贝却是和main方法中的a,b指向heap上同样的对象,StringBuffer是字符串可变操作类,你用append方法,改变的是heap上的对象,所以不管swap里的a 还是main中的a的"内容"都得到了改变,而swap中的b=a,只是改变了引用拷贝b的引用对象,和main函数中的b是没有关系的!我不知道我表达的LZ有没有理解。
11楼的图很nice,但是并没有解释到位(我还是有些疑惑)比如稍微改变一点代码public class Buffer
{void swap(StringBuffer a, StringBuffer b)
{
a.append(" more");
b = a;
}
public static void main(String[] args)
{
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
Buffer buffer = new Buffer();
//buffer.swap(a, b);
a.append(" more");
b=a; //java是指传递,因此将a的地址附给a并不能改变b的值
System.out.println(a+" \n"+b);
}
} 它的输出值还是一样!用#11如何去解释呢?
a.append(" more"); 改变a指向的heap中的StringBuffer对象为one more
b=a;使b指向和a同样的位于heap中的StringBuffer对象,所以也是one more
不知道你哪里不理解了?
附:18楼修改后的代码public class Buffer
{
void swap(StringBuffer a, StringBuffer b)
{
a.append(" more");
b = a;
}
public static void main(String[] args)
{
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
Buffer buffer = new Buffer();
//buffer.swap(a, b);
a.append(" more");
b=a;
//java是指传递,因此将a的地址附给a并不能改变b的值
System.out.println(a+" \n"+b);
}
}输出结果:
One more
One more
值虽然是不变的,但值指向的对象是会改变的,如果你想改变传进去的参数,请设计返回相应对象类型的方法f(),用b=o.f()这样的形式就可以了。