JAVA方法参数传递

时间:2022-11-04 21:24:50

package demo.methodparamDemo;

public class MethodParamsDemo {

public static void main(String[] args) {

int num = 10;
changeInt(num);
System.out.println("main:"+num);

boolean flag = true;
changeboolean(flag);
System.out.println("main:"+flag);


Boolean flag2 = true;
changeBoolean(flag2);
System.out.println("main:"+flag2);

String str = "str";
changeString(str);
System.out.println("main:"+str);

Student student = new Student("张三");
changeObj(student);
System.out.println("main:"+student.toString());
}

public static void changeInt(int i){
i = i+1;
System.out.println("method -changeInt:"+i);
}

public static void changeboolean(boolean flag){
flag = false;
System.out.println("method -changeboolean:"+flag);
}


public static void changeString(String str){
str = "new str ";
System.out.println("method -changeString:"+str);
}

public static void changeBoolean(Boolean flag){
flag = false;
System.out.println("method -changeBoolean:"+flag);
}

public static void changeObj(Student student){
student = new Student("李四");
System.out.println("method -changeObj:"+student.toString());
}

}

 

 

 

----------------------------run 

method -changeInt:11
main:10
method -changeboolean:false
main:true
method -changeBoolean:false
main:true
method -changeString:new str
main:str
method -changeObj:李四
main:张三