JAVA中逻辑运算符、位运算符举例扩展

运算符

  • 算数运算符:+ – * / % ++ –

  • 赋值运算符:=

  • 关系运算符: > < >= <= == !=instanceof

  • 逻辑运算符: && || ! 与或非

  • 位运算符: & | ^ ~ >> << >>>

  • 条件运算符:? :

  • 扩展赋值运算符:+= -= *= /=

    1. 加减乘除 + – * /

      package operator;
      
      public class Demo01 {
          public static void main(String[] args) {
              //二元运算符
              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(a/(double)b);   //  0.5  这个地方需要强转
      
    2. 赋值运算符以及计算的注意事项

      package operator;
      
      public class Demo02 {
          public static void main(String[] args) {
              long a =1125515l;
              int b = 123;
              short c = 10;
              byte  d = 8 ;
              System.out.println(a+b+c+d);   //这个变成lone类型
              System.out.println(b+c+d);     //第二个变成int类型
              System.out.println(c+d);     //第三个  变成 int类型
      
              //总结:如果俩个运算或者多个运算中有一个类型为Long那么结果就为long,其余的都为int.
              //同理。如果有一个为double那么结果就为double
              
             
      
    3. 关系运算符以及模运算

      package operator;
      
      public class Demo03 {
          public static void main(String[] args) {
              //关系运算符的表示结果:正确 错误   就是布尔值
      
              int  a = 10;
              int b =20;
      
              //这个是模运算
              int c =21;
              System.out.println(c%a);   //这个取余的用法意思就是  c/a的余数
      
      
      
      
              System.out.println(a>b);
              System.out.println(a<b);
              System.out.println(a==b);
              System.out.println(a!=b);
      
    4. 自增自减运算符以及模运算(单独抛出来)

      package operator;
      
      public class Demo04 {
          public static void main(String[] args) {
              // ++    --   自增    自减    一元运算符
      
              int a =3 ;
              int b = a++;    //a++   a =a+ 1    执行完这行代码后,先给b赋值。赋值完之后再自增
              int c = ++a ;  //++a               执行完这行代码前,先自增。在给b赋值。
              
                                                //(a--和--a同理 )
      
              System.out.println(a);
              System.out.println(b);
              System.out.println(c);
      
              //幂运算   2^3    2*2*2 = 8      会使用一些工具类来操作
              double pow = Math.pow(2, 3);
              System.out.println(pow);
      
    5. 与 或 非 运算

      package operator;
      
      public class Demo05 {
          public static void main(String[] args) {
              //逻辑运算符    与       或        非
              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++<4);
              System.out.println(d);
              System.out.println(c);
      
      
          }
      }
      
    6. 位运算 以及左移右移

      package operator;
      
      public class Demo06 {
          public static void main(String[] args) {
              /**
               *    A =     0011 1100
               *    B =     0000 1101
               *--------------------------------------
               *   (与)A&B=    0000 1100     //相同位的两个数字都为1,则为1;否则都为0。
               *    (或)A|B=    0011 1101    //相同位只要一个为1即为1;
               * (异或)A^B=  0011 0001    //相同位不同则为1,相同则为0。
               *  (取反) ~B=  1111 0010   //与上面的B相反
               *
               *        2*8怎么运算最快?              <<     >>       左移   右移
               * */
      
              System.out.println(2<<3);
          }
      }
      
      
    7. 扩展赋值运算符

      package com.huang.operator;
      
      public class Demo07 {
          public static void main(String[] args) {
              //  +=  -=  /=   *=
              int a = 10 ;
              int b =20;
              a+=b;
              System.out.println(a);
              System.out.println(b);
              System.out.println("--------------------------------");
              //字符串连接符  +
              System.out.println(a+b);
              System.out.println(""+a+b);    //   +号两侧  只要出现了String类型,也就是字符串类型,就会转换成String类型然后进行连接,
      
              System.out.println(a+b+"");  //把字符串放到后面就会直接相加
      
      
          }
      }
      
      
      
    8. 三元运算符

      package com.huang.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);
      
      
      
          }
      }
      

版权声明:本文为weartheword原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。