JAVA-笔试-关于StringBuffer和StringBuilder

public static void main(String[] args) {
        StringBuffer a = new StringBuffer("A");
        StringBuffer b = new StringBuffer("B");
        operate(a,b);
        System.out.println(a + "," + b);
    }
    public static void operate(StringBuffer x,StringBuffer y){
        x.append(y);
        y = x;
        y.append('x');
    }

答案为:

        ABx,B

解析:y=x之后y指向a的地址,即xy都指向a


public static void main(String[] args) {
        String textString = new String("java");
        StringBuffer textBuffer = new StringBuffer("java");
        stringReplace(textString);
        bufferReplace(textBuffer);
        System.out.println(textString + textBuffer);
    }
    public static void bufferReplace(StringBuffer text){
        text.append("C");
        text = new StringBuffer("Hello");
        text.append("World!");
    }

    public static void stringReplace(String text){
        text = text.replace('j','i');
    }

答案为:

        javajavaC


public static void main(String[] args) {
        String s = "bbbb";
        StringBuffer sb = new StringBuffer("bbbb");
        change(s,sb);
        System.out.println(s + sb);
    }

    public static void change(String s,StringBuffer sb){
        s = "aaaa";
        sb.setLength(0);
        sb.append("aaaa");
    }

答案为:

        bbbbaaaa


public static void main(String[] args) {
        String str = null;
        StringBuffer sb = new StringBuffer();
        sb.append(str);

        System.out.println(sb.length());

        System.out.println(sb);

        StringBuffer sb1 = new StringBuffer(str);
        System.out.println(sb1);
    }

答案为:

        4
        null
        Exception in thread "main" java.lang.NullPointerException

解析:查看源码

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值