Java面試題:
當一個對象被當作參數傳遞到一個方法後,此方法可改變這個對象的屬性,並可返回變化後的結果,那麼這裡到底是值傳遞還是引用傳遞?
答案基本上是:值傳遞
說明:得出這種結論的前提必須是“參數的值就是對該對象的引用,而不是對象的內容”
----------------------------------------------------
值傳遞:方法調用時,實際參數把它的值傳遞給對應的形式參數,方法中執行形式參數值的改變不影響實際參數的值。
引用傳遞:也稱為傳地址。方法調用時,實際參數的引用(即地址,而不是參數的值)被傳遞給方法中相對應的形式參數,
在方法中執行對形式參數的操作實際上就是對實際參數的操作(地址操作),這樣方法執行中形式參數值的改變將會影響實際參數的值。
值傳遞——基本數據類型作為參數(如:String,int)
- public static void changeInt(int a, int b){
- int tmp;
- tmp = a;
- a = b;
- b = tmp;
- }
參數調用:
- // int類型,值傳遞
- int a = 123;
- int b = 456;
-
- printInt(a, b);
- changeInt(a, b);
- printInt(a, b);
執行結果:
a = 123; b = 456
a = 123; b = 456
----------------------------------------------------
引用傳遞——對象或數組作為參數(如:Person,Array)
- class Person{
- String name;
- int age;
-
- public Person(String name, int age){
- this.name = name;
- this.age = age;
- }
-
- public void changePerson(Person person, String name, int age){
- person.name = name;
- person.age = age;
- }
-
- public void printPerson(Person person){
- System.out.println("name = " + person.name + "; age = " + person.age);
- }
- }
參數調用:
- // 對象,引用傳遞
- Person p = new Person("張三", 20);
-
- p.printPerson(p);
- p.changePerson(p, "李四", 30);
- p.printPerson(p);
執行結果:
name = 張三; age = 20
name = 李四; age = 30
----------------------------------------------------
完整示例:
- package com.val_ref;
-
- // Person類
- class Person{
- String name;
- int age;
-
- public Person(String name, int age){
- this.name = name;
- this.age = age;
- }
-
- public void changePerson(Person person, String name, int age){
- person.name = name;
- person.age = age;
- }
-
- public void printPerson(Person person){
- System.out.println("name = " + person.name + "; age = " + person.age);
- }
- }
-
- class iArray{
-
- public iArray(){
-
- }
-
- public void reverseArray(int []array){
- int tmp;
- int len = array.length;
- for(int i=0; i<len/2; i++){
- tmp = array[i];
- array[i]= array[len-1-i];
- array[len-1-i] = tmp;
- }
- }
-
- public void printArray(int []array){
-
- for(int i=0; i<array.length; i++){
- System.out.print(array[i] + " ");
- }
- System.out.println();
- }
- }
-
-
- public class testValue {
-
- public static void main(String []args){
-
- // 對象,引用傳遞
- Person p = new Person("張三", 20);
-
- p.printPerson(p);
- p.changePerson(p, "李四", 30);
- p.printPerson(p);
-
- // 對象,引用傳遞
- int []array = new int[5];
- for(int i=0; i<array.length; i++){
- array[i] = i+1;
- }
- iArray obj = new iArray();
- obj.printArray(array);
- obj.reverseArray(array);
- obj.printArray(array);
-
-
- // String類型,值傳遞
- String name1 = "張三";
- String name2 = "李四";
-
- printString(name1, name2);
- changeString(name1, name2);
- printString(name1, name2);
-
- // int類型,值傳遞
- int a = 123;
- int b = 456;
-
- printInt(a, b);
- changeInt(a, b);
- printInt(a, b);
- }
-
- // String類型
- public static void changeString(String name1, String name2){
- String tmpName;
- tmpName = name1;
- name1 = name2;
- name2 = tmpName;
- }
-
- public static void printString(String name1, String name2){
- System.out.println("name1 = " + name1 + "; name2 = " + name2);
- }
-
- // int類型
- public static void changeInt(int a, int b){
- int tmp;
- tmp = a;
- a = b;
- b = tmp;
- }
-
- public static void printInt(int a, int b){
- System.out.println("a = " + a + "; b = " + b);
- }
- }
執行結果:
name = 張三; age = 20
name = 李四; age = 30
1 2 3 4 5
5 4 3 2 1
name1 = 張三; name2 = 李四
name1 = 張三; name2 = 李四
a = 123; b = 456
a = 123; b = 456