scjp题目解析(八)
2008-07-28 22:15
344 查看
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:
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:
相关文章推荐