Day5

运算符

package operator;

public class Demo01 {
    public static void main(String[] args) {
        //二元运算符
        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/b);//输出为0 因为a和b是int型
        System.out.println(a/(float)b);//接上段代码 只要将B或者A强制转换成浮点型就行
    }

}
package operator;

public class Demo02 {
    public static void main(String[] args) {
        long a = 12312321231232123L;
        int b = 123;
        short c = 10;
        byte d = 8 ;
        System.out.println(a+b+c+d);//long
        System.out.println(b+c+d);//int
        System.out.println(c+d);//int
        //运算会自动升为混合运算类型中最高的类型


    }

}
package operator;

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

package operator;

public class Demo04 {
    public static void main(String[] args) {


    //  ++自增  --自减   一元运算符
        int a = 3;
        int b =a++;//执行完这行代码后,先给b赋值,再自增
        //a++ 即 a = a+1
        System.out.println(a);//输出4
        //a++  a=a+1;
        int c =++a;//执行完这行代码前,先自增。再给B赋值

        System.out.println(a);//输出5
        System.out.println(b);//输出3
        System.out.println(c);//输出5
        //总结  a++先赋值 再自增         ++a 先自增 再赋值

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

    }
}

 package operator;

//逻辑运算符
public class Demo05 {
    public static void main(String[] args) {
        //与(and)&&:运算前面是错的话,后面的步骤就不用执行了
        // 或(or)||:
        // 非(取反)!(a&&b):    */

        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));//两个为真,结果即为假,如果为假,即为假

        //短路运算
        int c = 5;
        boolean d = (c<4)&&(c++<4);/*如果C<4 那结果直接为假,所以运算短路了,后面(c++<4)就不会运算,那么输出C还是5;
                                    同理如果C>4,那么前半段判断为真,紧接着会判断后半段,所以C会加一,最后输出C等于6;*/
        System.out.println(d);
        System.out.println(c);
        //位运算
        /*
        A = 0011 1100
        B = 0000 1101

        A&B=0000 1100  都为真才是真,否则为假
        A|B=0011 1101  只要有一个为真的,就为真的
        A^B=0011 0001   相同为假,不同为真
        ~B=1111 0011    如果是真的输出为假,反之如此

        2*8 = 16         2*2*2*2
        效率极高!!!
        <<     *2
        >>     /2

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

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


    }

}
 
package operator;

public class Demo06 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        a+=b;//a=a+b
        System.out.println(a);

        //字符串连接符   +   ,加号运算符两侧 只要有一侧出现string(字符串类型)他就会把另外的操作处转化为string类型
        System.out.println(""+a+b);//输出1020

        System.out.println(a+b+"");//输出50   理由:运算顺序!!!如果字符串在后面,前面依旧会先进行运算
    }

}
package operator;

//三元运算符
public class Demo07 {
    public static void main(String[] args) {
        //x?y:z
        //如果x==true,则结果为y,否则结果为z
        int score = 80;
        int a  = 80;
        String type = score<60?"不及格":"及格";//必须掌握
        System.out.println(type);
    }
}

posted @ 2020-11-22 16:12  不秃头的程序猿  阅读(80)  评论(0)    收藏  举报