日期:2014-05-20  浏览次数:20631 次

帮我看下这道题
public class Test2 {
private static int j=0;
private static boolean methodB(int k){
j+=k;
return true;
}

public static void methodA(int i){
boolean b;
b=i<10|methodB(4);//|在这代表什么意思
b=i<10||methodB(8);//||在这代表什么意思是
}
public static void main(String[] args) {
methodA(0);
System.out.println(j);
}
}


------解决方案--------------------
b=i<10|methodB(4);//|在这代表什么意思 --代表位操作运算号,不会短路
b=i<10||methodB(8);//||在这代表什么意思是 --代表 逻辑运算符,运短路

这道题的methodB方法只会被调用一次
------解决方案--------------------
“或”运算。
a||b
a|b

a,b只要有一个是true,结果就是true
||,| 区别在于,前者,只要a是true就直接返回true,不用判断b了。

书上都有写。