static show()方法运行了吗?为什么看结果感觉没运行啊?

qq_16977471 2018-04-26 01:33:27
package com.itheima01;

public class Test {
public static void main(String[] args){

String s1 = "abc";

String s2 = "xyz";

show(s1,s2);

System.out.println(s1+"-----"+s2);

}

static void show(String s1,String s2){

s1 = s2+s1+"Q";

s2 = "W"+s1;
}
}
...全文
698 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hoff5230 2018-04-26
  • 打赏
  • 举报
回复
public class test { private static String s1; private static String s2; private static test instance = null; public static String getS1() { return s1; } public void setS1(String s1) { test.s1 = s1; } public static String getS2() { return s2; } public void setS2(String s2) { test.s2 = s2; } public test() { } public static test getInstance(){ if(instance == null){ instance = new test(); } return instance ; } public static void main(String[] args){ s1 = "abc"; s2 = "xyz"; show(s1,s2); System.out.println(s1+"-----"+s2); } private static void show(String s1, String s2) { s1 = s1+s2+"d"; s2 = s1+s2+"c"; test instance = test.getInstance(); instance.setS1(s1); instance.setS2(s2); } }
dong_19890208 2018-04-26
  • 打赏
  • 举报
回复
https://www.zhihu.com/question/31203609
dong_19890208 2018-04-26
  • 打赏
  • 举报
回复
java 方法都是值传递不是引用传递,所以你对传进show方法的变量修改不会影响main方法中的值的
suxiao_mo 2018-04-26
  • 打赏
  • 举报
回复
运行了,只不过 String s1,String s2在show方法中是一个局部变量,声命周期是show方法,然后你对s1,s2的任何操作,当方法结束时,就结束了。不会影响到你main方法中的s1,s2的值。
qq_34128895 2018-04-26
  • 打赏
  • 举报
回复
show()方法里的s1,s2是局部变量,而且没有返回值。 可以这样写:    String s1 = "abc";     String s2 = "xyz";     show(s1,s2); }    static void show(String s1,String s2){     s1 = s2+s1+"Q";     s2 = "W"+s1;  System.out.println(s1+"-----"+s2); }

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧