java中值傳遞的三種情況,形參的改變不影響實參
阿新 • • 發佈:2019-01-30
public static void main(String[] args) {
//第一種基本資料型別
int a= 1;
int b= 2;
System.out.println("a:"+a+"---"+"b:"+b);
change(a,b);
System.out.println("a:"+a+"---"+"b:"+b);
System.out.println("===============================");
//第二中引用型別中的字串
String s = "java";
System.out.println(s);
change2(s);
System.out.println(s);
System.out.println("===============================");
//第三中基本型別被包裝後的的包裝類(Integer,Character,其餘都是首字母大寫)
Integer i = new Integer(10);
Integer j = new Integer(20);
System.out.println(i+"-----"+j);
change3(i,j);
System.out.println(i+"-----"+j);
}
public static void change(int a,int b){
int temp = a;
a=b;
b=temp;
}
public static void change2(String s){
s="helloworld";
}
public static void change3(Integer i,Integer j){
int temp = i;
i=j;
j=temp;
}
//第一種基本資料型別
int a= 1;
int b= 2;
System.out.println("a:"+a+"---"+"b:"+b);
change(a,b);
System.out.println("a:"+a+"---"+"b:"+b);
System.out.println("===============================");
//第二中引用型別中的字串
String s = "java";
System.out.println(s);
change2(s);
System.out.println(s);
System.out.println("===============================");
//第三中基本型別被包裝後的的包裝類(Integer,Character,其餘都是首字母大寫)
Integer i = new Integer(10);
Integer j = new Integer(20);
System.out.println(i+"-----"+j);
change3(i,j);
System.out.println(i+"-----"+j);
}
public static void change(int a,int b){
int temp = a;
a=b;
b=temp;
}
public static void change2(String s){
s="helloworld";
}
public static void change3(Integer i,Integer j){
int temp = i;
i=j;
j=temp;
}
}
執行結果如下:
a:1---b:2
a:1---b:2
===============================
java
java
===============================
10-----20
10-----20