Java运算符

  • 在java运算中,在涉及到数据类型范围小于int的(如byte,char,short)时,数据类型会自动提升为int类型

算数运算符

加减乘除、取余

 public class Subtracting {
     public static void main(String[] args) {
         //二元运算符
         //Ctrl + D :复制当前行到下一行
         int a = 10;
         int b = 20;
 ​
         System.out.println(a+b);
         System.out.println(a-b);
         System.out.println(a*b);
         System.out.println(a%b);//取余
         System.out.println(a/(double)b);
         System.out.println("====================");
 ​
         //================================================
         //================================================
         long c = 123123123123L;
         int d = 123;
         short e = 10;
         byte f = 8;
         double g = 123456.123456;
 ​
         System.out.println(c+d+e+f);//Long
         System.out.println(d+e+f);//Int
         System.out.println(e+f);//Int
         System.out.println(e+f+g);//Double
         System.out.println(e+f+g);//Double
         //在java运算中,在涉及到数据类型范围小于int的(如byte,char,short)时,数据类型会自动提升为int类型
 ​
     }
 }
 ​

关系运算符

 public class Relation {
     public static void main(String[] args) {
         //关系运算符返回的结果:正确or错误,布尔值
         int a = 10;
         int b = 20;
 ​
         System.out.println(a>b);//false
         System.out.println(a<b);//true
         System.out.println(a==b);//false
         System.out.println(a!=b);//true
     }
 }

幂运算

 public class PowerOperation {
     public static void main(String[] args) {
         double pow=Math.pow(3,2);
         //Math.pow(3,2);Alt+回车
         System.out.println(pow);//结果为9
         //幂运算:运用数学工具类Math,pow:幂运算
     }
 }

逻辑运算符

 public class LogicalOperator {
     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));
         //逻辑非运算!():如果是假,则结果为真
         System.out.println("!(a || b:"+!(a||b));
         //逻辑非运算!():如果是真,则结果为假
     }
 }

自增自减

 public class ZiZeng_ZiJian {
     public static void main(String[] args) {
         //++ -- ; 自增、自减 ; 一元运算符
         int a = 3;
         int b = a++;//先给b赋值,再自增;
         //先把a赋予b,b=3,a在自加,a=a+1=4;
         int c = ++a;//先自增,再给c赋值;
         //先自加,a=a+1=5,b=3,再把a赋予c
 ​
         System.out.println(a);//5
         System.out.println(b);//3
         System.out.println(c);//5
     }
 }

拓展赋值运算符

 public class Expan {
     public static void main(String[] args) {
         int a = 10;
         int b = 20;
 ​
         a+=b;//a=a+b
         a-=b;//a=a-b
 ​
         System.out.println(a);
 ​
         //字符串连接符:+
         System.out.println(a+b);
         //String
         System.out.println(""+a+b);//结果为1020
         //+运算符前面出现String类型就会把后面的操作数转换为String
         System.out.println(a+b+"");//结果为30
         //String出现在后面则a+b依旧进行运算
     }
 }

位运算

 public class AnOperation {
     public static void main(String[] args) {
         /*
         A = 0011 1100
         B = 0000 1101
         -------------------------------------------
         A & B(与) = 0000 1100
         A | B(或) = 0011 1101
         ~ B  (非) = 1111 0010
         A ^ B(异或)=0011 0001(相同为0,不同为1)
          */
      
         //-----------------------------------------
        
         // 效率极高
         
         /*
         2*8 = 16 → 2*2*2*2
 ​
         <<     *2
         >>     /2
 ​
         0000 0000        0
         0000 0001        1
         0000 0010        2
         0000 0011        3
         ------------------
         0000 1000        8
         0001 0000        16
          */
         System.out.println(2<<3);//结果为16
     }
 }

三元运算符

 public class TernaryOperation {
     public static void main(String[] args) {
         // x ? y : z
         //如果x==true,则结果为y,否则结果为z
         //必须掌握
 ​
         int score = 80;
         String type = score < 60 ? "不及格" : "及格";
         System.out.println(type);
     }
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值