java中方法參數傳遞方式是按值傳遞。
-
如果參數是基本類型,傳遞的是基本類型的【字面量值的拷貝】。
-
如果參數是引用類型,傳遞的是該參量所引用的對象在堆中【地址值的拷貝】。
參考:
測試一:基本類型作爲參數傳遞
/**
* 基本類型作爲參數傳遞
*/
public class Test1 {
public static void main(String[] args) {
//基本類型作爲參數傳遞
int num = 2;
System.out.println("before change , num = " + num);
changeData(num);
System.out.println("after change , num = " + num);
}
public static void changeData(int num) {
num = 10;
}
}
//before change , num = 2
//after change , num = 2
測試二:對象作爲參數傳遞
/**
* 對象作爲參數傳遞
*/
public class Test2 {
public static void main(String[] args) {
//對象作爲參數傳遞
A a = new A("hello");
System.out.println("before change , a.str = " + a.str);
changeData(a);
System.out.println("after change , a.str = " + a.str);
}
public static void changeData(A a) {
a.str = "hi";
}
static class A {
public String str;
public A(String str) {
this.str = str;
}
}
}
//before change , a.str = hello
//after change , a.str = hi
測試三
import java.util.Arrays;
public class Test3 {
@Override
public String toString() {
return super.toString();
}
public static void main(String[] args) {
String str = new String("987");
StringBuilder stringBuilder = new StringBuilder("987");
char[] ch1 = new char[]{'1', '2', '3'};
char[] ch2 = new char[]{'1', '2', '3'};
change(str, stringBuilder, ch1, ch2);
System.out.println(str);
System.out.println(stringBuilder);
System.out.println(ch1);
System.out.println(ch2);
}
public static void change(String str, StringBuilder stringBuilder,
char[] ch1, char[] ch2) {
str = "987654";
stringBuilder.append("5");
ch1[0] = '0';
ch2 = new char[]{'9', '8', '7'};
}
}
//987
//9875
//023
//123
測試四:對象的引用傳遞
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public void setLocation(int x, int y) {
this.x = x;
this.y = y;
}
private static void modifyPoint(Point p1, Point p2) {
//此處拷貝對象的地址值
System.out.println(" modifyPoint內 交換前 p1============" + p1);
System.out.println(" modifyPoint內 交換前 p2============" + p2);
Point tmpPoint = p1;
p1 = p2;
p2 = tmpPoint;
System.out.println(" modifyPoint內 交換後 p1============" + p1);
System.out.println(" modifyPoint內 交換後 p2============" + p2);
//修改原有對象
p1.setLocation(6, 6);
//新建對象
p2 = new Point(5, 5);
System.out.println(" modifyPoint內 結果 p1============" + p1);
System.out.println(" modifyPoint內 結果 p2============" + p2);
}
public static void main(String[] args) {
Point p1 = new Point(1, 1);
Point p2 = new Point(2, 2);
System.out.println("modifyPoint前 p1============" + p1);
System.out.println("modifyPoint前 p2============" + p2);
modifyPoint(p1, p2);
System.out.println("modifyPoint後 p1============" + p1);
System.out.println("modifyPoint後 p2============" + p2);
System.out.println("[" + p1.x + "," + p1.y + "],[" + p2.x + "," + p2.y + "]");
}
}
//modifyPoint前 p1============Point@133314b
//modifyPoint前 p2============Point@b1bc7ed
// modifyPoint內 交換前 p1============Point@133314b
// modifyPoint內 交換前 p2============Point@b1bc7ed
// modifyPoint內 交換後 p1============Point@b1bc7ed
// modifyPoint內 交換後 p2============Point@133314b
// modifyPoint內 結果 p1============Point@b1bc7ed
// modifyPoint內 結果 p2============Point@7cd84586
//modifyPoint後 p1============Point@133314b
//modifyPoint後 p2============Point@b1bc7ed
//[1,1],[6,6]
- modifyPoint前
- modifyPoint內 交換前
- modifyPoint內 交換後
- modifyPoint內 結果