- public class Dmeo {
- public static void main(String[] args) {
- String s1 = "hello";
- String s2 = "world";
- System.out.println(s1 +"-------" + s2);//hello-----world
- change1(s1,s2);
- System.out.println(s1 +"-------" + s2);//world-----worldworld
-
- StringBuffer s3 = new StringBuffer("hello");
- StringBuffer s4 = new StringBuffer("world");
- System.out.println(s3 +"-------" +s4);//
- change2(s3,s4);
- System.out.println(s3 +"-------" +s4);
- }
- private static void change2(StringBuffer s3, StringBuffer s4) {
- s3 = s4;
- s4 = s3.append(s4);
- }
- private static void change1(String s1, String s2) {
- s1 = s2;
- s2 = s1 + s2;
- }
- }
复制代码
解释一下为何运行结果是这样的 |
|