package www.base;
/**
* 位运算符 & | ^(异或) ~ >> << >>>
* 条件运算符 ? : (三元运算符)
* 扩展运算符 += -= *= /=
* 字符串连接符 + : 如果+出现在左边,则字符串连接;如果出现在右边,则先运算后连接。
*/
public class Demo005_Operator1 {
public static void main(String[] args) {
/* 位运算
a = 0011 1100
b = 0000 1101
a&b = 0000 1100 (if both 1, result 1; otherwise result 0)
a|b = 0011 1101 (if either 1, result 1; otherwise result 0)
a^b = 0011 0001 (if same,result 0; otherwise result 1)
~b = 1111 0010 (if 1, result 0; if 0, result 1)
2*8=16 2*2*2*2
为了提高效率
<< 左移 *2 ; >> 右移 /2
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 0101 5
0000 1000 8
0001 0000 16
*/
System.out.println(2<<3);
System.out.println(2>>1);
System.out.println("==========================");
int a = 10;
int b = 20;
a+=b; // a = a + b
System.out.println(a);
a-=b; // a = a - b
System.out.println(a);
System.out.println("==========================");
System.out.println(""+a+b); //字符串连接符 + : 如果+出现在左边,则字符串连接;如果出现在右边,则先运算后连接。
System.out.println(a+b+""); //字符串连接符 + : 如果+出现在左边,则字符串连接;如果出现在右边,则先运算后连接。
System.out.println("==========================");
int score = 80;
String result = score < 60 ? "failed" : "success";
System.out.println(result);
}
}