分享

11

 印度阿三17 2021-02-13

11

运算符

算术运算符: ,-,*,/,%, ,--

赋值运算符:=

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

逻辑运算符:&&,||,!

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

条件运算符:?:

拓展赋值运算符: =,-=,*=,/=

算术运算符

public class Demo01 {
    public static void main(String[] args) {
        //二元运算符    小技巧:Ctrl D 复制当前行到下一行
        int a = 10;
        int b = 20;
        int c = 30;
        int d = 35;
        System.out.println(a b);
        System.out.println(a%b);//取余  这里相除余数是10,故输出10
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);//注意这里有小数,记得转换


    }
}

不同类型相加,有long类型就输出long类型,没有则输出int类型

总之比int高的类型存在就输出高的类型,没有就输出int

public class Demo02 {
    public static void main(String[] args) {
        int a = 123;
        long b = 11233333333L;
        byte c = 11;
        short d =222;
        System.out.println(a b c d);//long
        System.out.println(a c d);//int
        System.out.println( c d);//int
    }

}

,--

public class Demo04 {
    public static void main(String[] args) {
        //自增 ,自减    ,--
        //一元运算符
        int a = 10;
        int b = a  ;
        // 先给b赋值,再让a 1
        int c =   a;
        //先给a 1,再让c赋值
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        //很多数学运算都使用Math工具
        double d =Math.pow(2,3);//幂运算
        System.out.println(d);
    }
}

关系运算符

public class Demo03 {
    public static void main(String[] args) {
        //关系运算符返回的结果:正确 or 错误,   布尔值
        //if
        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);
    }
}

逻辑运算符

public class Demo05 {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
        System.out.println(a&&b);//&&  与,都是真则为真
        System.out.println(a||b);//||  或,有真则为真
        System.out.println(!b);//!    取反,真变假,假变真

        //短路运算:与运算中,监测到假就停止运算
        int c = 5;
        boolean d = (c  <4)&&(c<4);
        System.out.println(c);
        System.out.println(d);//此时c已经自增,结果输出6
        //调换顺序
        int e = 5;
        boolean f = (e<4)&&(e  <4);
        System.out.println(e);
        System.out.println(f);
        //此时输出5,说明短路了

    }
}

位运算符

public class Demo06 {
    public static void main(String[] args) {
        /*
        位运算,底层代码,二进制
        A = 1000 0111
        B = 1100 0010
        ---------------------
        A&B=1000 0010
        A|B=1100 0111
        A^B=0100 0101
        !B=0011 1101

        2*8 怎么样运算快 =2*2*2*2
        二进制数字
        1左移一位代表*2
        1右移一位代表/2
        这样算效率极高
        <<   >>  左移    右移
         */
        System.out.println(2<<3);//输出16
    }
}

拓展赋值运算符

偷懒用的

public class Demo07 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        a =b;//a=a b
        System.out.println(a);
        a-=b;//a=a-b
        System.out.println(a);
        //字符串连接符       String
        System.out.println(a b "");//输出30,意为ab相加
        System.out.println("" a b);//输出1020,意为字符串连接


    }
}

三元运算符

public class Demo08 {
    public static void main(String[] args) {
        //三元运算符  x ? y:z
        //x=true,输出y,否则,输出z
        int score = 80;
        String ef =score>60?"及格":"不及格";
        System.out.println(ef);
    }
}

学习自狂神说Java

来源:https://www./content-4-855901.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多