1.对象传递
package test; class test{ private String name; private String age; public test(String name) { this.name=name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getInfo() { return"姓名:"+name+" "+"年龄:"+age; } } public class 关键字 { public static void main(String[] args) { test t=new test("张三"); fun(t); System.out.println(t.getName());//李四 } public static void fun(test demo) { demo.setName("李四"); } }
2.传递String
public class 关键字 { public static void main(String[] args) { String s="wangwang"; fun(s); System.out.println(s);//wangwang } public static void fun(String str) { str="caizhen"; } }
3.传递基本数据类型
public class 关键字 { public static void main(String[] args) { int s=520; fun(s);//参数传递,不涉及内存传递 System.out.println(s);//520 } public static void fun(int temp) {//int temp=520,当时s还是520 temp=666; } }