StringBuffer与","(""也可)相加

本文探讨了Java中StringBuffer作为方法参数时的行为特点,特别是当方法内部修改StringBuffer对象时对外部调用的影响。通过一个具体示例,解释了为何在方法中改变StringBuffer引用不会影响原始对象,但对其内容的修改则会体现出来。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 static void operate(StringBuffer x, StringBuffer y) {
  y.append(x);
  y = x;
 }
}

 

 What   is   the   result?  
   
  A.The   code   compiles   and   prints   “A,B”.  
  B.The   code   compiles   and   prints   “A,   BA”.    
  C.The   code   compiles   and   prints   “AB,   B”.    
  D.The   code   compiles   and   prints   “AB,   AB”.    
  E.The   code   compiles   and   prints   “BA,   BA”.    
  F.The   code   does   not   compile   because   “+”   cannot   be   overloaded   for   stringBuffer.

ans:B

if: System.out.println(a + b);  then ans:F

StringBuffer与","(""也可)相加时,会自动调用StringBuffer.toString()函数的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值