逻辑运算符 三元运算符 位运算符

逻辑运算符

package base.operator;
//逻辑运算符
public class Dome05 {
  public static void main(String[] args) {
      //与(and)或(or)
      boolean a=true;
      boolean b=false;
      System.out.println("a&&b:"+(a&&b));//逻辑与运算,两个都为真
      System.out.println("a||b:"+(a||b));//逻辑或运算,两个有一个为真
      System.out.println("!(a&&b):"+!(a&&b));//如果是真则变为假
  //短路运算
      int c=5;
      boolean d=(c<4)&&(c++<4);//c<4是非,所以后面不执行c还是5
      System.out.println(d);
      System.out.println(c);
  }
}  

位运算符

package base.operator;
public class Dome06 {  
public static void main(String[] args) {    
//位运算      
/*      
A=0011 1100      
B=0000 1101    
A&B=0000   1100      
A|B=0011 1101        
A^B=0001      
~B=1111 0010      
2*8 =16   2*2*2*2        
<<   *2      
>>   /2        
*/        
System.out.println(2<<3);  
}
}

逻辑运算符

package base.operator;
//逻辑运算符
public class Dome07 {  
public static void main(String[] args) {      
int a=10;        
int b=20;      
a+=b;//a=a+b      
System.out.println(a);        
//字符串连接符   +        
System.out.println(""+a+b);//字符串在前后面会进行拼接    
System.out.println(a+b+"");//字符串在后前面会进行运算  
}
}

三元运算符

package base.operator;
//三元运算符
public class Dome08 {  
public static void main(String[] args) {      
//x?y:z        
//如果x==true,则结果为y,否则结果为z        
int score =80;//score是成绩的意思        
String type=score<60?"不及格":"及格";      
System.out.println(type);
}
}

关于运算符的优先级一定要用()表示出来,这样就会明明白白

posted @ 2022-04-30 17:42  怎样的人生  阅读(52)  评论(0)    收藏  举报