1.逻辑运算符



1 //逻辑运算符
2 public class Demo04 {
3 public static void main(String[] args) {
4 // 与:&&(有假为假) 或:||(有真为真) 非:!(取反)
5 boolean a=true;
6 boolean b=false;
7
8 System.out.println("a && b:"+(a&&b));
9 System.out.println("a || b:"+(a||b));
10 System.out.println("a || b:"+!(a&&b));
11 }
12 }


 

2.位运算符



1 //位运算符
2 public class Demo05 {
3 public static void main(String[] args) {
4 /*
5 A=0011 1100
6 B=0000 1101
7
8 A&B=0000 1100
9 A|B=0011 1101
10 A^B=0011 0001 //相同为0,不相同为1
11 ~B=1111 0010 //取反
12
13 效率极高
14 << *2
15 >> /2
16
17 0000 0000 0
18 0000 0001 1
19 0000 0010 2
20 0000 0011 3
21 0000 0100 4
22 0000 1000 8
23 0001 0000 16
24 */
25 System.out.println(2<<3); //输出16
26 }


 

3.字符串的连接



1 public class Demo06 {
2 public static void main(String[] args) {
3 int a=10;
4 int b=20;
5
6 //字符串连接符 + ,String
7 System.out.println("string="+a+b); //输出1020
8 System.out.println(a+b+"=string"); //输出30
9 }
10 }


 

4.三元运算符



1 //三元运算符 ? :
2 public class Demo07 {
3 public static void main(String[] args) {
4 //x ? y:z 如果x==true,那么y,否则结果为z (必须掌握)
5
6 int score01=90;
7 String grade = score01>70 ?"及格":"不及格";
8
9 System.out.println(grade);
10 }
11 }