运算符

package operator;
public class Demo5 {
//与 或 非
public static void main(String[] args) {
boolean a = true;
boolean b = false;

System.out.println("a && b:"+(a && b));//逻辑与运算,两个变量都为真,结果才为true
System.out.println("a || b:"+(a || b));//逻辑或运算,有一个为真,结果为true
System.out.println(" ! (a && b):"+!(a && b));//真变假,假变真

//短路运算,//前面c<4为错,后面不执行
int c =5;
boolean d = (c<4)&&(c++<4);
System.out.println(d);
System.out.println(c);
}
}

package operator;

public class Demo6 {
public static void main(String[] args) {
/*
A= 0011 1100
B= 0000 1101
--------------------
A&B = 0000 1100 //A与B,两个都为1是1,两个都为0是0,否则为0
A|B = 0011 1101 //A或者B,都是0为0都是1为1,其中一个是1就是1
A^B = 0011 0001 //取反,相同为0,否则为1
~B = 1111 0010//取反,取B的反

28位运算非常的快,效率高
<< >> 指向哪边往哪边移
左移相当于
2,右移相当于/2
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
*/
System.out.println(2<<3);
}
}

package operator;

public class Demo7 {
public static void main(String[] args) {
int a = 10;
int b = 20;

a+=b;//a=a+b
a-=b;//a=a-b

System.out.println(a);

//字符串连接符 + ""string字符串
System.out.println(a+b);
System.out.println(""+a+b);
System.out.println(a+b+"");//字符串在后面不运算
}
}

package operator;
//三元运算符
public class Demo8 {
public static void main(String[] args) {
// X? Y:Z
//X为真,则输出Y,否则输出Z

int score = 80;
String type = score < 60 ? "不及格":"及格";
// if
System.out.println(type);

}
}

posted @ 2021-10-12 21:37  xxm555  阅读(41)  评论(0)    收藏  举报