StringBuffer作为参数传递

package com.shujia.day11;

public class stringBufferDemo1 {
    public static void main(String[] args) {
        String s1 = "hello";

        //StringBuffer作为参数传递
        StringBuffer sb1 = new StringBuffer("hello");
        StringBuffer sb2 = new StringBuffer("world");
        System.out.println("sb1: "+sb1+", sb2: "+sb2);
        fun1(sb1,sb2);
        System.out.println("sb1: "+sb1+", sb2: "+sb2);

    }
    public static void fun1(StringBuffer sb1,StringBuffer sb2){
        sb1 = sb2;
        sb2.append(sb1);
        System.out.println("sb1: "+sb1+", sb2: "+sb2);
    }
}
结果为
sb1: hello, sb2: world
sb1: worldworld, sb2: worldworld
sb1: hello, sb2: worldworld

posted @ 2024-08-08 22:29  ていせい  阅读(3)  评论(0编辑  收藏  举报