基本运算符

运算符

  • java语言支持如下运算符: 优先级()
    • 算数运算符:+,-,*,/,%(模运算,取余),++,–
    • 赋值运算符:=
    • 关系运算符:<,>,<=,>=,==,!= ,instanceof
    • 逻辑运算符:&&,||,!(与,或,非)
    • ==========================================================
    • 位运算符:&,|,^(异或),~(取反),>>,<<,>>>
    • ==========================================================
    • 条件运算符?:
    • 扩展赋值运算符:+=,-=,*=,/=
  • 算术运算:
public class Dome04 {
    public static void main(String[] args){
    //    二元运算符
    //    Ctrl+D:复制当前行到下一行
        int a = 10;
        int b = 20;
        int c= 25;
        int d= 30;

        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);//强转(有小数,默认int,转为浮点型)
        System.out.println(a%b);//取模
    }


    }
  • 自增自减:

    public class Demo06 {
        public static void main(String[] args){
            //自增,自减
            int a=9;
            int b=a++;//a++一元运算
            int c=++a;
            int d=--a;
            int e=a--;
    
            System.out.println(a);//9
            System.out.println(b);//9
            System.out.println(c);//11
            System.out.println(d);//10
            System.out.println(e);//10
        //    b=a++先赋值,再自增,b=++a,先自增再赋值;
        //    幂运算  2^3   2*2*2=8  Math,很多运算我们会使用一些工具类来操作!
            double pow = Math.pow(3,9);
            System.out.println(pow);
        }
        }
    
  • 逻辑运算符:

    public class Demo07 {
        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:"+(!a));//false  如果是真,则变为假;如果为假,则变为真;
    
            //短路运算
            int i=6;
            boolean d=((i<1)&&(i++<10));//&&前面(i<1)已经为假了则i++不会执行了
            System.out.println(d);
            System.out.println(i);
            System.out.println("=================================================================");
    
            int i2=6;
            boolean d1=((i2<7)||(i2--==5));
            System.out.println(d1);
            System.out.println(i2);
            
            
           //异或,
            int n=4;
            int m=8;//不用第三方变量讲两个值转换
    
            /*n=n+m
            * m=n-m
            * n=n-m
             但n,m值相加可能超int
            * */
            System.out.println(n=n^m);
            System.out.println(m=n^m);
            System.out.println(n=n^m);
        }
    
    
       
    }
    
  • 位运算符

public class Demo08 {
    public static void main(String[] args){
        //位运算
    /*
    A=0011 0011
    B=0010 1001;
    =======================
    A&B=0010 0001;
    A|B=0011 1011;
    (异或,相同为0,不同为1)A^B=0001 1010
    ~A=1100 1100
    =========================
    2*8=16  2*2*2*2
    <<(左移)
    >>(右移)

    0000 0001    1
    0000 0010    2
    0000 0100    4
    0000 1000    8
    0001 0000   16
    0010 0000    32

    左移<<每移一位相当于*2
    右移>>每移一位相当于/2

    位运算效率高*/

    }
}
  • 赋值运算符
public class Demo09 {
    public static void main(String[] args){
    int a=1;
    int b=3;

    a+=b;//a=a+b
        System.out.println(a);
    a-=b;//a=a-b
        System.out.println(a);
     a*=b;//a=a*b
        System.out.println(a);
     a/=b;//a=a/b
        System.out.println(a);
    }
    }
  • 条件运算符

    public class Demo10 {
        public static void main(String[] args){
            //x ? y : z
            //如果x为真,则结果为y,如果x为假 则结果为z
           int score=90;
            String type = score>60?"及格":"不及格";//掌握
            //if
            System.out.println(type);
    
    
    
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值