JAVA的学习日记5

java类型转换复习


package operator;

public class Demo02 {
    public static void main(String[] args) {
        //强类型转换
        long a = 2121231231321L;
        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);    //short
    }
}

运算符

关系运算符


package operator;

public class Demo03 {
    public static void main(String[] args) {
        //关系运算符返回的结果:正确、错误  布尔值类型
        int a = 10;
        int b = 20;
        int c = 32;

        System.out.println(a>b);    //false
        System.out.println(a<b);    //true
        System.out.println(a==b);   //false
        System.out.println(a!=b);   //true
        //除法【取整数部分】
        System.out.println(c/a);   //3
        //取余【取小数部分】
        System.out.println(c%a);   //2
    }
}

一元运算符和Math类


package operator;

public class Demo04 {
    public static void main(String[] args) {
        // ++、--    自增、自减运算符,也叫作一元运算符
        int a = 3;


        int b = a ++;   //a ++表示意思为a = a + 1;
        int c = ++ a;   //++ a 表示为a = a + 1;
        /*a ++ 和++ a 的区别:
         *   a ++ 表示先把a的值赋值为等号前的数,如上面的b,然后再执行a = a + 1的操作,最后结果为:
         *   b是a++之前的值,而a是a++之后的值,Eg:此时的b值为3,a值为4
         *   ++ a 表示先执行a = a + 1的操作,再把a的值赋值为等号前的数,如上面的c,最后结果为:
         *   c是++a之后的值,而a是++a之后的值,Eg:此时的c值为5,a值为5*/
        System.out.println(a);      //5
        System.out.println(b);      //3
        System.out.println(c);      //5


        //Math类
        //Math.pow中的pow表示幂运算,Eg:2^3 = 2*2*2 = 8
        double pow = Math.pow(2,3);
        System.out.println(pow);    //8
    }
}

特别说明:
instanceof 是 Java 的一个二元操作符,类似于 ==,>,< 等操作符。
instanceof 是 Java 的保留关键字。它的作用是测试它左边的对象是否是它右边的类的实例,返回 boolean 的数据类型。
这个要等到面向对象的时候在说明了

逻辑运算符

package operator;

public class Demo05 {
    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&&b):"+(!(a&&b)));   //true      如果是真,则结果为假,如果为假,则结果为真

        //短路运算
        int c = 5;
        boolean d = (c < 4) && (c++ < 4);
        System.out.println(d);      //false
        System.out.println(c);      //5

    }
}

位运算符

package operator;

public class Demo06 {
    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*2*2
        位运算就是在二进制的基础上进行的,效率极高
        0000 0001   1
        0000 0010   2
        0000 0011   3
        0000 0100   4
        0000 1000   8
        0001 0000   16

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

拓展赋值运算符

package operator;

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

        //拓展赋值运算符 += -= *= /=
        int a = 10;
        int b = 20;
        int c = a += b;
        int d = a -= b;
        int e = a *= b;
        int f = a /= b;

        System.out.println(c);  //30
        System.out.println(d);  //10
        System.out.println(e);  //200
        System.out.println(f);  //10

        //字符串连接符 +
        System.out.println(a+b);    //30    表示为运算
        System.out.println(""+a+b); //1020  表示为拼接
    }
}

条件运算符【三元运算符】

package operator;

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

        //条件运算符 ?:  又称为三元运算符
        //x ? y : z
        //表示为如果x为true,则结果为y,否则结果为z

        int score = 80;
        String type = score < 60 ? "不及格" : "及格";
        System.out.println(type);   //及格
    }
}

Java中运算符优先级

原文地址:https://www.cnblogs.com/cdoudou/p/13636018.html