逻辑运算符的具体运算就不细讲了,数电都学过。这里主要注意java里的逻辑运算符是双写
与:&& 或:|| 非:!
位运算符 与:& 或:| 异或:^
位运算的效率极高 << 左移 表示乘2 >> 右移 表示除以2
public class Demo06 {
public static void main(String[] args) {
/*
* A=0011 1100
* B=0000 1101
* 位运算
* A&B
* A|B
* A^B异或
* ~B
* 2*8=16 2*2*2*2
* 位运算 效率极高
* << 左移 表示乘2
* >> 右移 表示除以2
* */
System.out.println(2<<3);
}
}
2<<3的输出为16
public class Demo07 {
public static void main(String[] args) {
int a=10;
int b=20;
a+=b;//a=a+b,最好不要写a+=b,不要偷懒!
System.out.println(a);
//字符串连接符 +
System.out.println(a+b);
System.out.println(""+a+b);//如果Sring在前面,最后输出都会转化为String类型
System.out.println(a+b+"");//这个的输出仍然为运算结果
注意这段代码最后的输出的类型
三元运算符
x?y:z if也能表达同样的意思,但前者更为精简
public class Demo08 {
public static void main(String[] args) {
//x?y:z
//如果x==true,则结果为y,否则结果为z
int score=50;
String type = score<60?"不及格":"及格";//还是可以用if,但这个代码更精简
System.out.println(type);
}
}