A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. public class StringBufferDemo {
  2.         public static void main(String[] args) {
  3.                 String s1 = "hello";
  4.                 String s2 = "world";
  5.                 System.out.println(s1 + "---" + s2);// hello---world
  6.                 change(s1, s2);
  7.                 System.out.println(s1 + "---" + s2);// hello---world

  8.                 StringBuffer sb1 = new StringBuffer("hello");
  9.                 StringBuffer sb2 = new StringBuffer("world");
  10.                 System.out.println(sb1 + "---" + sb2);// hello---world
  11.                 change(sb1, sb2);
  12.                 System.out.println(sb1 + "---" + sb2);// hello---worldworld

  13.         }

  14.         public static void change(StringBuffer sb1, StringBuffer sb2) {
  15.                 sb1 = sb2;
  16.                 sb2.append(sb1);
  17.         }

  18.         public static void change(String s1, String s2) {
  19.                 s1 = s2;
  20.                 s2 = s1 + s2;
  21.         }
  22. }
复制代码
注意:
1.String作为参数传递,效果和基本类型作为参数传递是一样的。



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马