运算符

运算符

运算符

package com.bbing.base;

public class Demo3 {
    public static void main(String[] args) {
        long a=1232433522333L;
        int b=123;
        short c=10;
        byte d=8;

        //几个操作数中有一个为Long,输出就为Long;如果没有Long,结果都为Int.
        //同理,如果有Double,输出即为double.
        System.out.println(a+b+c+d);//Long
        System.out.println(b+c+d);//int
        System.out.println(c+d);//int
        System.out.println("========================");


        //++  --  自增 自减  一元运算符
        int f=3;
        int g=f++;//执行完这行代码后,先给g赋值,再自增
        //f=f+1;
        int h=++f;//执行完这行代码后,先自增,再给h赋值
        //f=f+1;

        System.out.println(f);
        System.out.println(g);
        System.out.println(h);
        System.out.println("========================");

        //幂运算2^3  2*2*2=8;
        double pow = Math.pow(2, 3);//Math.pow+回车--快捷键返回值Alt+Enther
        System.out.println(pow);
        System.out.println("========================");

        //与(and) 或(or) 非(取反)
        boolean j = true;
        boolean k = false;

        System.out.println("j && k:"+(j&&k));
        System.out.println("j || k:"+(j||k));
        System.out.println("!j && k:"+!(j&&k));//逻辑非运算:如果是真,则变为假;如果是假,则变为真。
        System.out.println("========================");
        //短路运算
        int l = 5;
        boolean i = (l<4)&&(l++<4);
        System.out.println(i);
        System.out.println(l);
        System.out.println("========================");

    }
}

package com.bbing.base;

public class Demo4 {
    public static void main(String[] args) {
        /*
        A = 0011 1100
        B = 0000 1101
        ---------------------
        A&B = 0000 1100
        A|B = 0011 1101
        A^B = 0011 0001
        ~B = 1111 0010

        2*8 = 16
        <<  *2
        >>  /2

         */

        System.out.println(2<<3);
        System.out.println("========================");

        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);
        System.out.println(a+b+"");

        //x ? y : z
        //如果x==true,则结果为y,否则结果为z
        int score = 50;
        String type = score < 60 ? "不及格" : "及格";//必须掌握
        System.out.println(type);

    }
}
上一篇:练习2-11 计算分段函数[2] (10分)


下一篇:一些运算符