运算符
public static void main(String[] args) {
//与(and)或(or)非(取反)
boolean a = true;
boolean b = false;
//逻辑与运算:两个变量都真,结果才为true
System.out.println("a&&b:"+(a&&b));
//逻辑或运算:两个变量有一个为真,结果才为true
System.out.println("a||b:"+(a||b));
//如果是真则变为假,如果是假则变为真
System.out.println("!(a&&b:)"+!(a&&b));
}
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
------------------------------------------------------------
A&B = 0000 1100 A B上下对应的两个数都为 1得 1,否则为 0
A|B = 0011 1101 A B上下对应的两个数都为 0得 0,否则为 1
A^B = 0011 0001 A B上下对应的两个数都相同为 0,否则为 1
~B = 1111 0010 A B上下对应的两个数都为 1得 0,否则为 1
------------------------------------------------------------
2*8 = 16 2*2*2*2
位运算的效率极高!
<< (左移) 相当于 *2
>> (右移) 相当于 /2
0 0000 0000
1 0000 0001
2 0000 0010
4 0000 0100
8 0000 1000
16 0001 0000
*/
System.out.println(2<<3);
}
public static void main(String[] args) {
int a = 10;
int b = 20;
//a = a+b
a+=b;
System.out.println(a);
//a = a-b
a-=b;
System.out.println(a);
//字符串连接符 + ,String
System.out.println(""+a+b); //字符串在前后运算按 String类型自动拼接
System.out.println(a+b+""); //字符串在后,先执行运算然后拼接字符串
}
public static void main(String[] args) {
//三元运算符
// x ? y : z
//如果 x==true 则输出 y,否则输出 z
int score = 80;
String type = score <=60 ?"及格":"不及格";//虽然是一种偷懒行为,但必须要掌握
System.out.println(type);
}