- class Test{
- public static void main(String[] args) {
- String s1 = "java";
- String s2 = "hello";
- 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 void method_1(String s1,String s2){
- s1.replace('a','k');
- s1 = s2;
- }
- public static void method_2(StringBuilder s1,StringBuilder s2){
- s1.append(s2);
- s1 = s2;
- }
复制代码
在方法1和2中 都有s1=s2; 为什么输出结果没变。
我把方法1中的s1=s2;直接写到主函数中,结果发生变化。 有点蒙,求解
- public static void main(String[] args) {
- String s1 = "java";
- String s2 = "hello";
- s1.replace('a','k');
- s1 = s2;
- // method_1(s1,s2);
- System.out.println(s1+"...."+s2);
- //hello....hello
复制代码 |
|