Given:
public class Test1 {
static void doubling(Integer ref, int pv) {
ref = 20;
pv = 20;
}
public static void main(String[] args) {
Integer iQbj = new Integer(10);
int iVar = 10;
doubling(iQbj++, iVar++);
System.out.println(iQbj + "," + iVar);
}
}
What is the result?
A.
11, 11
B.
10, 10
C.
21, 11
D.
20, 20
E.
11, 12
題解
由於Java永遠為「pass by value」,因此doubling方法內對於參數ref和pv的變化並不會影響到外面的變數。這題只需注意到程式第11行,將iQbj和iVar做了加1的動作,因此輸出為:
11, 11