zoukankan      html  css  js  c++  java
  • Java基础04-运算符

    运算符

    一、运算符简介
    1. 算术运算符: +、-、*、/、%、++、-- 【加减乘除、模运算(取余数)、自增、自减】

    2. 赋值运算符:= 【int a = 10】

    3. 关系运算符:>、<、>=、<=、==、!=、instanceof 【大于、小于、大于等于、小于等于、等于、不等于、用来判断某个实例是不是某个类的实例】

    4. 逻辑运算符:&&、||、! 【与、或、非(取反)】

    5. 位运算符: &、|、^、~、>>、<<、>>> 【了解即可!】

    6. 条件运算符:?、: 【三元运算符,了解即可!】

    7. 扩展复制运算符:+=、-=、*=、/= 【了解即可!】

    8. 总结:优先级最高(),建议经常使用()来规整代码

    二、算术运算
    //  运算示例+-*/
    int a = 10;
    int b = 20;
    int c = 25;
    int d = 25;
    ​
    System.out.println(a+b);    //30
    System.out.println(a-b);    //-10
    System.out.println(a*b);    //200
    System.out.println(a/b);    //运算出现小数,需要使用double类型,0
    System.out.println(a/(double)b);    //正解,0.5
    // 运算示例% ++ ——
    System.out.println(c%a);    //c/a余数=1,所以输出为1
    //++ 自增 一元运算符
    //-- 自减 一元运算符
    int a = 3;
    int b = a++;    //先将a赋值给b,然后执行a = a + 1
    System.out.println(a);  //4
    int c = ++a;    //先执行a = a + 1,再将结果赋值给c
    ​
    System.out.println(a);  //5
    System.out.println(b);  //3
    System.out.println(c);  //5
    // 幂运算处理 2^3=2*2*2如下所示,很多运算我们会使用一些工具类去操作(如math类)
    double pow = Math.pow(2,3);
    System.out.println(pow);    //8.0
    三、算术运算-数据类型拓展
    long a = 1212121212121212L;
    int b = 123;
    short c = 10;
    byte d = 8;
    ​
    System.out.println(a + b + c + d);    //输出Long类型,1212121212121353
    System.out.println(b + c + d);      //输出int类型,141
    System.out.println(c + d);        //输出int类型,18
    // 结论1,如果运算中只要有Long类型参与,则结果就是Long类型
    // 结论2,如果运算中没有Long类型参与,不论是否有int类型参与,则结果就是int类型
    四、关系运算-(>、<、==)
    //关系运算符返回的结果: 正确 or 错误
    int a = 10;
    int b = 20;
    int c = 21;
    ​
    System.out.println(a>b);
    System.out.println(a<b);
    System.out.println(a==b);
    五、逻辑运算
    // 与(and) 或(or) 非(取反)
    boolean a = true;
    boolean b = false;
    ​
    System.out.println("a && b:"+(a&&b) );  //逻辑与运算,真真得真、真假得假,a && b:false
    System.out.println("a || b:"+(a||b) );  //逻辑或运算,真假得真、假假得假、真真得真,a || b:true
    System.out.println("!(a  && b):"+!(a&&b) ); //逻辑非运算,非真即假,!(a  && b):true
    //拓展:短路运算
    int c = 5;  
    ​
    //说明&&后面这段自增代码没有运行,换句话说,与运算中上来就是假,那后面就不会计算了
    boolean d = (c<4)&&(c++<4); 
    ​
    System.out.println(d);  //false
    System.out.println(c);  //5
    六、位运算
    /*
    A = 0011 1100
    B = 0000 1101
    ​
    ---------------------------------------
    A&B 0000 1100   11得1、10得0、00得0
    A|B 0011 1101   00得0、01得1、11得1
    A^B 0011 0001   11得0、00得0、10得1(异或)
    ~B  1111 0010   0取反得1、1取反得0(取反)
    ---------------------------------------
    ​
    2*8 = 16 怎样让计算机最快运行
    !!!位运算效率是极高!!!
    左移<< 相当于*2
    右移>> 相当于/2
    ​
    二进制示例 十进制
    0000 0000 0
    0000 0001 1
    0000 0010 2
    0000 0011 3
    0000 0100 4
    0000 0101 5
    0000 0110 6
    0000 0111 7
    0000 1000 8
    ...
    0001 0000 16
    ...
    */
    
    
    七、拼接符号
    int a = 10;
    int b = 20;
    ​
    a += b; //a = a+b
    a -= b; //a = a-b
    ​
    System.out.println(a);
    ​
    // 字符串连接符号 +
    System.out.println(a+b);    //将a+b=10+20
    System.out.println(""+a+b);    //将 空字符串拼接10拼接20=1020
    System.out.println(a+b+"");    //先将 10+20=30 再拼接空字符串
    八、条件运算(三元运算符)
    // x ?  y : z   是比较偷懒得流程控制写法,叫做三元运算
    //如果x==true,则结果为y,否则结果为z
    int score = 40;
    String type = score < 60 ? "不及格" : "及格";    //必须掌握
    System.out.println(type);   //不及格
    九、内存溢出
    //操作比较大的数的时候,逐一溢出问题
    // jdk7新特性,数字之间可以用下划线分割
    int money = 1000000000;
    int money1 = 10_0000_0000;
    System.out.println(money);  //1000000000
    System.out.println(money1); //1000000000
    int years = 20;
    int total = money*years;    
    System.out.println(total);  //输出 -1474836480,计算溢出了
    //正解,不溢出
    long total2 = money*((long)years);  //-1474836480
    System.out.println(total2);         //20000000000

     

  • 相关阅读:
    fzuoj Problem 2177 ytaaa
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Capture the Flag
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Convert QWERTY to Dvorak
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest May Day Holiday
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Demacia of the Ancients
    zjuoj The 12th Zhejiang Provincial Collegiate Programming Contest Ace of Aces
    csuoj 1335: 高桥和低桥
  • 原文地址:https://www.cnblogs.com/lich1x/p/12984832.html
Copyright © 2011-2022 走看看