作者forris (乔巴)
看板java
标题[问题] SCJP 练习题
时间Mon Jan 7 16:47:34 2008
What will happen if you attempt to compile and run the following code
public class TimDig{
public static void main(String argv[]){
TimDig td = new TimDig();
td.samcov();
}
public void samcov(){
int i=1;
int j=2;
if((i==20) && (j==(i=i*2))){
}
System.out.print(i);
if((i==20) & (j==(i=i*2))){}
System.out.print(i);
int x = i & 2;
System.out.print(x);
}
}
the answer is
Compilation and output of 122
我想问 print(x) 的结果,怎麽不是 1 (i=1 成立)或是 0 (作 AND 运算)?
刚刚我又发现一个很有趣的事:就是
if((i==20) & (j==(i=i*2))){}
System.out.print(i);
这段程式中,把 {} 拿掉,或是 } 放到 System.out.print(i); 之後,
原本 i 值就不见了。这是怎麽一回事?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.127.81.30
※ 编辑: forris 来自: 140.127.81.30 (01/07 16:59)
1F:推 PsMonkey:& 也可以是位元运算 01/07 16:56
2F:→ forris:嗯,那如果是位元运算,就作 AND 运算...1 AND 2 不是 0 吗? 01/07 16:59
3F:→ dangerousman:因为第二小题 1 & 0 = 0 if 里面不执行, 所以不会印 10/03 16:33