运算符号

时间:2021-08-08
本文章向大家介绍运算符号,主要包括运算符号使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

运算符 算术运算符

package operator;

public class Domo01 {
   /*public static void main(String[] args) {
       // 二元运算符 Ctrl+d 复制当前行到下一行
       int a=10;
       int b=20;
       int c=27;
       int d=25;

       System.out.println(a+b);
       System.out.println(a-b);
       System.out.println(a*b);
       System.out.println(a/(double)b);*/
   public static void main(String[] args) {
       long a =123123123123123L;
       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);//int
       // 没有long时,所有非int类型转为int型

       int w=10;
       int e=20;
       int q=21;
       System.out.println(q%w);
       System.out.println(w>e);
       System.out.println(w<e);
       System.out.println(w==e);
       System.out.println(w!=b);

  }


  }

自增自减一元运算

package operator;

public class Domo0501 {
  public static void main(String[] args) {
      //自增自减 ++ --
      int a = 3;
      int b = a++; //a等于a加1 先把a值赋值给b=3,最后得出自增a=4   先赋值给b在运算
      int c = ++a;//a等于a加1 执行完这行代码前,先自增,再给b赋值   运算好在赋值给c
      System.out.println(a);
      System.out.println(b);
      System.out.println(c);


      //幂运算
      double pow = Math.pow(2, 3);
      System.out.println(pow);
  }

}

逻辑运算

package operator;

public class Domo0502 {
  // 逻辑运算符
  public static void main(String[] args) {
      // 与 或 非
      boolean a = true;
      boolean b = false;
      System.out.println("a && b: "+(a&&b)); //逻辑与 :两个变量都为真 结果为真
      System.out.println("a && b: "+(a||b));// 逻辑或 :两个变量有一个为真,则结果为真
      System.out.println("a && b: "+!(a&&b));//如果是真,则变为假,假变为真
      //短路运算
      int c=5;
      boolean d = (c<4)&&(c++<4);
      System.out.println(d);
      System.out.println(c);

  }
}

位运算

package operator;

public class Domo0504 {
  public static void main(String[] args) {
      /*
      A = 0011 1100
      B = 0000 1101
      A&B 00000 1100
      A|B 0011 1101
      A^B 0011 0001
      ~B 1111 0010


      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
        */
      System.out.println(2<<3); // 2*2^3
  }
}

三算运算 条件运算符

 

package operator;

public class Domo0504 {
public static void main(String[] args) {
int a = 10;
int b =20;
a+=b; //a =a + b
System.out.println(a);
// 字符串连接符 +

System.out.println(""+a+b);// 字符串在前面后面会进行拼接
System.out.println(a+b+"");// 字符串在后面前面会进行计算 面试题
// 三元运算符 x ? y: z
// 如果x==true ,则结果为y,否则结果为z
int score = 80;
String type = score < 60 ? "不及格":"及格";
System.out.println(type);
}
}

原文地址:https://www.cnblogs.com/ao127/p/15114959.html