JasonLuc1Fer

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

运算符

  1. 算数运算符:+,-,*,/,%,++,--
  2. 赋值运算符 :=
  3. 关系运算符:>, <, >=, <=, ==, !=, instanceof
  4. 逻辑运算符:&&,||,!
  5. 位运算符:&,|,^, ~, >>, <<, >>>
  6. 条件运算符: ?,:
  7. 扩展赋值运算符:+=,-=,*=,/=
package operator;

public class demo1 {
    public static void main(String[] args) {
        //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((double) a/b);
    }
}
package operator;

public class demo2 {
    public static void main(String[] args) {
        long a =1212315641313L;
        int b =123;
        short c =10;
        byte d =8;
        System.out.println(a+b+c+d); // long,有long类型在内结果也是long
        System.out.println(b+c+d); // int
        System.out.println(c+d); //int 无int类型在内结果也是int
    }
}
package operator;
//关系运算符
public class demo3 {
    public static void main(String[] args) {
        //关系运算符返回的结果: 正确 错误  布尔值
        int a = 10;
        int b = 20;
        int c = 21;
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
        System.out.println(c%a); //%取余,也叫模运算 21/10 =2 ...1
    }
}
package operator;

public class demo4 {
    public static void main(String[] args) {
        //++  自增 --  自减  一元运算符
        int a =3;
        int b =a++;
        // a++  a = a+1  执行完这行代码后,先给b赋值,再自增
        System.out.println(a); //a = 4 b = 3
        int c =++a;
        //++a  a = a+1  执行完这行代码前,先自增,再给c赋值
        System.out.println(a); //a =5 c=5
        System.out.println(b);
        System.out.println(c);

        //幂运算
        double n = Math.pow(2,3);
        System.out.println(n);
        double xx =Math.pow(3,4);
        System.out.println(xx);
    }
}
package operator;
//逻辑运算符 && ,||, !
public class demo5 {
    public static void main(String[] args) {
        //与(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)));
        //逻辑非运算:如果是真则为假,如果是假则为真
        //短路运算,判定前面是错误的后面的就不再运行了
        int c =5;
        boolean d =(c<4)&&(++c<7);
        System.out.println(c);
        System.out.println(d);

    }
}
package operator;
//位运算符
public class demo6 {
    public static void main(String[] args) {
        /*
    A = 0011 1100
    B = 0000 1101
    ------------------------
    A&B = 0000 1100
    A|B = 0011 1101
    A^B = 1100 0001
    ~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 1000  8
    0001 0000  16
     */
        System.out.println(2<<3);
    }
}
package operator;
//扩展赋值运算符
public class demo7 {
    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
        System.out.println(""+a+b); //1020
        System.out.println(a+b+""); //30+""
    }
}
package operator;
//三元运算符  条件运算符 ? :
public class demo8 {
    public static void main(String[] args) {
        // x ? y : z
        //如果x ==true,则结果为y,否则结果为z
        int score =80;
        String type1 = score < 60 ? "不及格":"及格";
        String type2 = score >= 80 ? "优秀":"待定";
        System.out.println(type1);
        System.out.println(type2);
    }
}

posted on 2021-11-16 16:27  JasonLuc1Fer  阅读(51)  评论(0)    收藏  举报