- public static void main(String[] args) {
- String s1 = "java";
- String s2 = "hello";
- s1 = method_1(s1, s2);//获取返回值,修改内容
- System.out.println(s1 + "...." + s2); // java....hello
- StringBuilder s11 = new StringBuilder("java");
- StringBuilder s22 = new StringBuilder("hello");
- method_2(s11, s22);
- System.out.println(s11 + "-----" + s22); // javahello-----hello
- }
- public static String method_1(String s1, String s2) {
- /*
- *replace()不会直接改变对象值,你需要获取其返回值
- */
- // s1.replace('a', 'k');
- // System.out.println(s1);//这样输出结果没变
- // s1 = s2;//函数里面 传进来是 形参,赋值不会影响实参
- return s1.replace('a', 'k');
- }
- public static void method_2(StringBuilder s1, StringBuilder s2) {
- s1.append(s2);//这边修改的是: 引用s1(形参和实参指向同一个对象)指向的对象内容(在堆里面),所以会影响实参
- // s1 = s2;//同上解释
- }
复制代码 结果:
|