day11-基本运算符

运算符

  • java语言支持如下运算符: 优先级 ( 多敲,多练习 )

    • 算术运算符:+,-,*,/,%(模运算:取余),++,--

     package operator;
     
     public class Demo1 {
         public static void main(String[] args) {
             //二元运算符
             //ctrl+d复制当前行到下一行
             
             int a=10;
             int b=20;
             int c=30;
             int d=40;
     
             System.out.println(a+b);
             System.out.println(a-b);
             System.out.println(a*b);
             System.out.println(a/b);//0     int
             System.out.println(a/(double)b);//0.5
        }
     }
     
     package operator;
     
     public class Demo4 {
         public static void main(String[] args) {
             
             //++ --   自增 自减
             
             int a=3;
             int b=a++;//执行完这行代码后   先赋值,再自增a=a+1
             //a++ ——> a=a+1
             System.out.println(a);//4
             int c=++a;//执行完这行代码后   先自增a=a+1,再赋值
     
             System.out.println(a);//5
             System.out.println(b);//3
             System.out.println(c);//5
     
             //幂运算 2^3 ——>2*2*2=8   很多时候调用Java里的工具类来做运算
             double pow = Math.pow(2, 3);
             System.out.println(pow);//8.0
        }
     }
     

     

    • 赋值运算符:=

     package operator;
     
     public class Demo2 {
         public static void main(String[] args) {
             long a=121212121212L;
             int b=120;
             short c=20;
             byte d=8;
     
             System.out.println(a+b+c+d);//Long
             System.out.println(b+c+d);//int
             System.out.println(c+d);//int
             //两个或者以上操作数据中有一个数为Long,则结果为Long,没有Long则为int
     
        }
     }
     

     

    • 关系运算符:>,<,>=,<=,==,!=,instanceof

     package operator;
     
     public class Demo3 {
         public static void main(String[] args) {
             //关系运算符返回的结果:正确 错误   布尔值
             // 常和if一起使用
     
             int a=10;
             int b=20;
             int c=25;
     
             // % 取余 模运算
             System.out.println(c%a);//5
     
             System.out.println(a>b);//false
             System.out.println(a<b);//true
             System.out.println(a==b);//false
             System.out.println(a!=b);//true
        }
     }
     

     

    • 逻辑运算符:&&,||,!(与,或,非)

       package operator;
       
       //逻辑运算符
       public class Demo5 {
           public static void main(String[] args) {
               //与&&(and)   或||(or)   非!(取反)
               boolean a=true;
               boolean b=false;
       
               System.out.println("a&&b:"+(a&&b));//false       两真才真,一假则假
               System.out.println("a||b:"+(a||b));//true       一真则真,两假才假
               System.out.println("!a&&b:"+!(a&&b));//true
       
               System.out.println("========================================");
               //短路运算
               int c=5;
               boolean d=(c<4)&&(c++<4);//理论上c在运算后应该为6
       
               // 与&&运算,有一个为错就直接判定为false,前面为false则不再向后看
       
               System.out.println(d);//false
               System.out.println(c);//5
          }
       }
       

       

    • 位运算符:&,|,……,~,>>,<<,>>>(了解!!!)

     package operator;
     
     public class Demo6 {
         public static void main(String[] args) {
             /*
             A = 0011 1100
             B = 0000 1101
          与 A&B = 0000 1100
          或 A|B = 0011 1101
        亦或 A^B = 0011 0001   相同则为0,否则为1
             ~B = 1111 0010
     
             <<左移   相当于*2
             >>右移   相当于/2
     
             2*8=16   2*2*2*2=16
             0000 0001   1
             0000 0010   2
             0000 0011   3
             0000 0100   4
             0000 0101   5
             0000 0110   6
             0000 0111   7
             0000 1000   8
             …………………………………
             0001 0000   16
     
              */
             System.out.println(2<<3);//16
             //位运算,效率极高!
        }
     }
     

     

    • 条件运算符:?=

     package operator;
     
     //三元运算符
     public class Demo8 {
         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);//及格
        }
     }
     

     

    • 扩展赋值运算符:+=,-=,*=,/=

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

posted @   小璐_哈基米  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示