运算符

运算符

image-20220929172658468

算术/赋值/关系运算符

        //二元运算符
        //Ctrl + D :复制当前行到下一行
        int a = 10;
        int b = 20;
        int c = 25;
        int d = 25;

        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);//注意int要转成double
        //关系运算符返回的结果: 正确,错误   布尔值
        //经常搭配 if 语句
        int a = 10;
        int b = 20;
        int c = 21;

        System.out.println(c%a); // c / a    21 / 10 = 2...1

        System.out.println(a>b);//false
        System.out.println(a<b);//true
        System.out.println(a==b);//false
        System.out.println(a!=b);//true
        // ++   --    自增,自减  一元运算符
        int a = 3;

        int b = a++; //执行完这行代码后,先给b赋值,再自增
        //a = a + 1;
        System.out.println(a); //4

        //a = a + 1;
        int c = ++a; //执行完这行代码前,先自增,再给b赋值

        System.out.println(a); //5
        System.out.println(b); //3
        System.out.println(c); //5

        //拓展:幂运算 2^3  2*2*2 = 8   很多运算我们会使用一些工具类来操作
        double pow = Math.pow(3, 2);
        System.out.println(pow);

逻辑运算符

        // 与(and)   或(or)    非(取反)
        boolean a = true;
        boolean b = false;

        System.out.println("a && b:"+(a && b));//逻辑与运算:两个变量都为真,结果才为true
        System.out.println("a || b:"+(a || b));//逻辑或运算:两个变量有一个为真,则结果才为true
        System.out.println("!(a && b):"+!(a && b));//如果是真,则变为假,  如果是假,则为真

        //短路运算 (比如在逻辑与运算中:a为false 那么b则不用判断,直接输出false)
        int c = 5;
        boolean d = (c<4)&&(c++<4);//用来验证 “短路运算” 用c++来判断c++ --> 5 + 1 = 6  发现没有运算
        System.out.println(d);
        System.out.println(c); //5

位运算符

    /*
    二进制理解次方计算    ">>"   "<<"
    A = 0011 1100
    B = 0000 1101
    -------------------------
    A&B = 0000 1100
    A|B = 0011 1101
    A^B = 0011 0001    相同为0,不相同为1
    ~B = 1111 0010     取反

    2*8 = 16   2*2*2*2
    效率极高!!!(二进制)
    <<    *2
    >>    /2

    0000 0000       0
    0000 0001       1
    0000 0010       2
    0000 0011       3
    0000 0100       4
    0000 0101       5
    0000 1000       8
    0001 0000       16

     */
        System.out.println(2<<3);//2*2*2 = 16

条件运算符(三元运算符)

//三元运算符
// x ? y : z
//如果 x == true,则结果为y,否则结果为z

int score = 50;
String type = score < 60 ? "不及格" : "及格"; //重点掌握
// if
System.out.println(type);

拓展赋值运算符

int a = 10;
int b = 20;

a+=b; //a = a+b
a-=b; //a = a-b

System.out.println(a);

//字符串连接符  +  ,String
System.out.println(""+a+b);//注意:当有String类型且在前面,后有 + 连接 表示字符串连接  “a+b”没有运算
System.out.println(a+b+"");//a+b运算

注意点

        long a = 1231211221232165153L;
        int b = 123;
        short c = 10;
        byte d = 8;
        double e = 12.12;

        System.out.println(a+b+c+d);//输出long类型,里面一个数是long类型输出的也是long
        System.out.println(b+c+d);//int
        System.out.println(c+d);//int
        System.out.println(b+c+d+e);//double
        //字符串连接符  +  ,String
        System.out.println(""+a+b);//注意:当有String类型且在前面,后有 + 连接 表示字符串连接  “a+b”没有运算
        System.out.println(a+b+"");//a+b运算

将类移动到建好的包中

没有移动成功类的看看下面有没有Do Refactor,有就点击它就可以了

image-20220929154442050
posted @ 2022-09-29 22:46  玩泥巴的熊  阅读(9)  评论(0)    收藏  举报