java基础知识03--运算符

运算符

  • 算术运算符

    +,-,*,/,++,–

    % 模运算:取余

  • 赋值运算符

    =

  • 关系运算符

    <, >, >=, <=, ==, !=, instanceof

  • 逻辑运算符

    &&(and), ||(or),!(not)

    And: true+true = false, or false

    Or: false+false = false, or true

    Not: not false = true; not true = false

    short circuit operation

    Boolean a = true;

    Boolean b = false;

    b&&a : because b is false, no need to do next part, the answer is definitely false.

            int c = 5;
            boolean d = (c<4) && (c++<4);
            System.out.println(d); // false
            System.out.println(c); // 5 cause (c++ < 4) has not executed
    
  • 位运算符

    &,|,^, ~, >>, <<, >>>

    A = 0011 1100
    B = 0000 1101
    
    A&B 0000 1100 (A and B both are 1, result is 1, or 0)
    A|B 0011 1101 (A and B both are 0, result is 0, or 1)
    A^B 0011 0001 (XOR A and B is same, result is 0, or 1)
    ~B  1111 0010 (negate)
    

    << 左移 乘2 >>右移 除2

    2*8 = 16 2*2*2*2
    << move left *2
    >> move right /2
    // high efficiency
    0000 0000 0
    0000 0001 1
    0000 0010 2
    0000 0011 3
    0000 0100 4
    0000 1000 8
    0001 0000 16
    
    System.out.println(2<<3); // 16
    
  • 条件运算符

    ? :

public class demo08 {
    public static void main(String[] args) {
        // ternary operator
        // ? :
        // x ? y : z
        // if x == true, result is y, otherwise result is z
        int score = 50;
       String type =  score < 60 ? "not pass" : "pass";
        System.out.println(type); // not pass
    }
}
  • 扩展赋值运算符

    +=, -=, *=, /=

    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        a += b; // a = a + b
        a -= b; // a = a - b
        System.out.println(a);
    }
        // string connector
        System.out.println(a+b); // 20
        System.out.println(""+a+b); // 1020 turn a and b to string, then connect
        System.out.println(a+b+""); // 30

tips

public class demo02 {
    public static void main(String[] args) {
        long a = 12142314315121L;
        int b = 123;
        short c = 10;
        byte d = 8;
        // if has long type, the type of result is long
        // or the type is int
        //if has a number whose type is double, the result is also double type
        System.out.println(a+b+c+d); // Long
        System.out.println(b+c+d); // int
        System.out.println(c+d); // int
        System.out.println((double)c+d); // double

    }
}

++ –

一元运算符 uniary operator

++:self-increasing

–: self-decreasing

public class demo04 {
    public static void main(String[] args) {
        // ++ self-increasing
        // -- self-decreasing
        int a = 3;
        int b = a++; // assign value to b first, then self-increasing
        // a++ is a = a + 1
        System.out.println(a); // 4
        // ++a is a = a + 1 previously
        // self-increasing first, then assign value to c
        int c = ++a;
        System.out.println(a); // 5
        System.out.println(b); // 3
        System.out.println(c); // 5
        
      // use some tool class to calculate
        double pow = Math.pow(2,3);
        System.out.println(pow);
    }
}

优先级

()

优先级

转载自:https://blog.csdn.net/xiaoli_feng/article/details/4567184

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读