java 试题(|和||区别)

来源:互联网 发布:2k18球员数据更新 编辑:程序博客网 时间:2024/05/22 00:16

public class Test {

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);}

}

What is the result?
A. The program prints “0”
B. The program prints “4”
C. The program prints “8”
D. The program prints “12”
E. The code does not complete

解析

b = i<10 | methodB(4); //中间为或计算符,执行完method(4)后,j=4
b = i<10 || methodB(8);//中间为“短路或”,因为i<10 已成立,不需要后续计算
知识点:
&&(短路与),&(与),|(或),||(短路或)。
&&和&都是表示与,区别是&&只要满足第一个条件,后面条件就不再判断。而&要对所有的条件都进行判断。

1 0
原创粉丝点击