位运算符、条件运算符、字符串连接符
/*不要轻易用位运算,很容易出错
A = 0011 1100
B = 0000 1101
A&B = 0000 1100(A与B,每一位进行比较,全1则1,有0则0)
A|B = 0011 1101(A或B,有1则1,全0则0)
A^B = 0011 0001(异或,相同为0.不同为1)
波浪线+B = 1111 0010(取反)
面试题:2*8如何运算最快??
<<左移(箭头指向左边),>>右移(箭头指向右边)
* */
System.out.println(2<<3);//输出结果是16,是将2的二进制左移3位,得出16
//0000 0001左移三位变成0000 1000
//条件运算符
int a = 10;
int b = 20;
a+=b;//a = a+b;
a-=b;//a = a-b;
//字符串连接符:+,在输出的时候,如果+两侧有一侧出现了String字符串类型,那么+就起连接作用
System.out.println(a+b);//输出30
System.out.println(""+a+b);//输出1020,+不再是加减的功能,而是起到连接的作用;
//面试题:下面两种的输出区别是什么?
System.out.println(""+a+b);//输出1020
System.out.println(a+b+"");//输出30,由于字符串在后面,前面还是正常进行加号运算
//三元运算符?:(x?y:z)表示如果x==true,则结果为y,否则结果为z
int score = 70;
String type = score<60?"不及格":"及格";
System.out.println(type);//及格

浙公网安备 33010602011771号