public class Happy {
public static void main(String args[]) {
int i=4;
int j=2;
methodA(i,j);
System.out.println(i);
}
static public void methodA(int i,int j) {
i<<=j;
}
}
What will be the output ?
a) compilation error
b) 16
c) 64
d) 4
f) runtime exception
以下題目寫個程序來測試一下
public class Eddy {
public static void main(String args[]){
int i=4;
int j=2;
methodA(i,j);
System.out.println(i);
}
static public void methodA(int i,int j) {
i<<=j;
}
}
解析如下:
首先把4化成二進制數
4/2=2 0
2/2=1 0
100左移兩位就是10000=(decimal)16
i<<=j然後把16重新賦值給j(注意:這裏的<<=意思是左移完之後把值賦給j =是賦值操作符==是等於操作符)
而這裏的i卻一直沒有變
Therefore, this issue is the result of:
D: