学习java第九天
package aperator;
//逻辑运算符
public class Dome05 {
//与and 或or 非取反
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,两个变量都是假结果才为true
System.out.println("! (a && b):"+!(a&&b));//如果是真,则变为假,如果假则变为真,取反后变成先假后增
//短路运算
int c = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(d);
System.out.println(c);
package aperator;
public class Dome06 {
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就是1
A^B = 0011 0001 a取反b 相同是0 不想同为1
~B = 1111 0010 完全相反
面试题
2*8=16 2*2*2*2最快速度计算 位预算
效率极高
< < *2
>> /2
0000 0000
0000 0001
0000 0010
0000 0011
0000 0100
0000 1000
0001 0000
*/
System.out.println(2<<3);
}
}
package aperator;
//字符串连接符 +
public class Dome07 {
public static void main(String[] args) {
int a =10;
int b =20;
a+=b;//a = a+b
a-=b;//a = a-b(学习阶段不要偷懒,不建议使用)
//字符串 +,出现string 都转换成string 链接起来
System.out.println(a);
System.out.println(a+b);
//以下两个一不一样
System.out.println(""+a+b);//字符在前,后面会拼接
System.out.println(a+b+"");//字符在后,先运算再拼接
}
}
package aperator;
//三元运算符
public class Dome08 {
public static void main(String[] args) {
//x ? y : z
//如果x==true,则结果为y,否则结果为z
int score = 80;
String type = score<60 ?"不及格":"及格";//(偷懒的)必须掌握精简
//if语句
System.out.println(type);
//java有优先级使用括号
}
}
浙公网安备 33010602011771号