目录
一、&
与&&
的区别
(一)&
(位与运算符)
&
是位运算符,用于对两个整数的每一位进行与运算。只有当两个位都为1时,结果位才为1。
public class BitwiseANDExample {
public static void main(String[] args) {
int a = 5; // 二进制:0101
int b = 3; // 二进制:0011
int result = a & b;
System.out.println("结果的二进制:" + Integer.toBinaryString(result)); // 输出:0001
System.out.println("结果的十进制:" + result); // 输出:1
}
}
(二)&&
(逻辑与运算符)
&&
是逻辑运算符,用于布尔表达式。只有当两个表达式都为true
时,整个表达式才为true
。&&
具有短路特性,即如果第一个表达式为false
,则第二个表达式不会被计算。
public class LogicalANDExample {
public static void main(String[