java-StringBuffer涉及引用传值操作探讨

提出问题:

Demo1
public class StringBufferTest {
	public static void main(String[] args) {
		StringBuffer strbf1=new StringBuffer("csdn is good!");
		StringBuffer strbf2=new StringBuffer("i am good!");
		new StringOperate().operateStringBuffer(strbf1,strbf2); //调用方法对两个StringBuffer对象进行操作
		System.out.println(strbf1);
		System.out.println(strbf2);
	}

}
class StringOperate{
	public void operateStringBuffer(StringBuffer strbf1,StringBuffer strbf2){//这里传递对象的引用
		strbf1.append("yes");  //在原对象的内存块上追加yes字符串
		strbf2.append("yes");
		System.out.println(strbf1);
		System.out.println(strbf2);	
	}
}

很明显,Demo1是对对象的引用操作

//out

csdn is good!yes
i am good!yes
csdn is good!yes
i am good!yes

Demo2
public class StringBufferTest {
	public static void main(String[] args) {
		StringBuffer strbf1=new StringBuffer("csdn is good!");
		StringBuffer strbf2=new StringBuffer("i am good!");
		new StringOperate().operateStringBuffer(strbf1,strbf2);
		System.out.println(strbf1);
		System.out.println(strbf2);
	}

}
class StringOperate{
	public void operateStringBuffer(StringBuffer strbf1,StringBuffer strbf2){
		strbf1 = strbf2;  //将strbf1的引用指向strbf2的内存块
		System.out.println(strbf1);
		System.out.println(strbf2);
		strbf1.append("yes");
		strbf2.append("yes");
		System.out.println(strbf1);
		System.out.println(strbf2);	
	}
}

Demo2相对Demo1添加了引用的修改,那么两次append的操作都会对主函数中的strbf2有影响,即添加了yesyes,对主函数的strbf1没影响。

//out

i am good!
i am good!
i am good!yesyes
i am good!yesyes
csdn is good!
i am good!yesyes

Demo3
public class StringBufferTest {
	public static void main(String[] args) {
		StringBuffer strbf1=new StringBuffer("csdn is good!");
		StringBuffer strbf2=new StringBuffer("i am good!");
		new StringOperate().operateStringBuffer(strbf1,strbf2);
		System.out.println(strbf1);
		System.out.println(strbf2);
	}

}
class StringOperate{
	public void operateStringBuffer(StringBuffer strbf1,StringBuffer strbf2){
		strbf2 = new StringBuffer("you are good!");//将strbf2重新指向一个新开辟的空间
		System.out.println(strbf1);
		System.out.println(strbf2);
		strbf1.append("yes");
		strbf2.append("yes");
		System.out.println(strbf1);
		System.out.println(strbf2);	
	}
}

//out

csdn is good!
you are good!
csdn is good!yes
you are good!yes
csdn is good!yes
i am good!

Demo3相对Demo1添加了将strbf2重新指向新开辟的空间,那么在主函数中strbf1会受append的影响,追加了yes,而strbf2不会受影响。

Demo4
public class StringBufferTest {
	public static void main(String[] args) {
		StringBuffer strbf1=new StringBuffer("csdn is good!");
		StringBuffer strbf2=new StringBuffer("i am good!");
		new StringOperate().operateStringBuffer(strbf1,strbf2);
		System.out.println(strbf1);
		System.out.println(strbf2);
	}

}
class StringOperate{
	public void operateStringBuffer(StringBuffer strbf1,StringBuffer strbf2){
		strbf1 = strbf2;
		System.out.println(strbf1);
		System.out.println(strbf2);
		strbf2 = new StringBuffer("you are good!");
		System.out.println(strbf1);
		System.out.println(strbf2);
		strbf1.append("yes");
		strbf2.append("yes");
		System.out.println(strbf1);
		System.out.println(strbf2);	
	}
}

综上所述,就可以得到Demo4的结果了。

//out

i am good!
i am good!
i am good!
you are good!
i am good!yes
you are good!yes
csdn is good!
i am good!yes

附加JDK1.6 API

   public synchronized StringBuffer append(String str) {//in class StringBuffer
	super.append(str);//append
        return this;
    }
    public AbstractStringBuilder append(String str) {  //in class AbstractStringBuilder 
	if (str == null) str = "null";
        int len = str.length();
	if (len == 0) return this;
	int newCount = count + len;
	if (newCount > value.length)
	    expandCapacity(newCount); //expandCapacity
	str.getChars(0, len, value, count);//getChars
	count = newCount;
	return this;
    }
    void expandCapacity(int minimumCapacity) { in class AbstractStringBuilder 
	int newCapacity = (value.length + 1) * 2;
        if (newCapacity < 0) {
            newCapacity = Integer.MAX_VALUE;
        } else if (minimumCapacity > newCapacity) {
	    newCapacity = minimumCapacity;
	}
        value = Arrays.copyOf(value, newCapacity); //Arrays.copyof
    }
    public static char[] copyOf(char[] original, int newLength) { //Arrays.copyof in class Arrays
        char[] copy = new char[newLength];
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }
    public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { //in class String
        if (srcBegin < 0) {
            throw new StringIndexOutOfBoundsException(srcBegin);
        }
        if (srcEnd > count) {
            throw new StringIndexOutOfBoundsException(srcEnd);
        }
        if (srcBegin > srcEnd) {
            throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
        }
        System.arraycopy(value, offset + srcBegin, dst, dstBegin,
             srcEnd - srcBegin);
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值