运算符学习

运算符

 

运算符

%取余运算 叫做 模

赋值运算符把右边的赋值给左边的

!= 不等于

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

算术运算符 - 二元运算符

package operator;

public class Demo01 {
   public static void main(String[] args) {
       // 二元运算符
       // 快捷键: command + d: 复制当前行到下一行 [Mac]
       int 10;
       int 20;
       int 25;
       int 25;

       System.out.println(a+b);
       System.out.println(a-b);
       System.out.println(a*b);
       System.out.println(a/(double)b); // 生命类型
  }

}

 

package operator;

public class Demo02 {
   public static void main(String[] args) {
       long 123123123123123L;
       int 123;
       short 10;
       byte 8;

       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, 一定要在转换的时候声明类型

       //没有long的计算都为int
       //cast 转换
  }
}

 

关系运算符

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

package operator;

public class Demo03 {
   public static void main(String[] args) {
       // 关系运算符返回的结果: 正确,错误 布尔值 True/False

       int 10;
       int 20;
       int 21;

       System.out.println(b);
       System.out.println(b);
       System.out.println(== b);
       System.out.println(!= b); // 和if配合使用

       //取余运算 模运算
       System.out.println(c%a); //余数为1


  }
}

 

 

一元运算符

package operator;

public class Demo04 {
  public static void main(String[] args) {
      // ++ 自增 ; -- 自减 一元运算符
      int a = 3;
      int b = a++; //对整体的a加1
      System.out.println(a); // 在这里a已经变成4了
      int c = ++a; // 在上一个a已经加1的基础上再加1。
      // a++ and ++a的区别; a-- and --a的区别

      System.out.println(a);
      System.out.println(b);
      System.out.println(c);

      // 幂运算 2^3 = 8; Java里没有 ^
      //很多运算,会使用一些工具"类"来操作
      double pow = Math.pow(2,3);
      System.out.println(pow);
       
  }
}

数学类:

Math类

 

a++和++a的区别

a++和++a的区别是a++是使用的a后,再对a进行加1。++a是先把a加1,然后再使用a。

 

逻辑运算符

&& || !

package operator;
//逻辑运算符 && || !

public class Demo05 {
   public static void main(String[] args) {
       // &&and ||or !not 取反
       boolean true;
       boolean false;
       System.out.println("a && b" +(a&&b)); // 逻辑与运算中,两个变量都为真,则为真
       System.out.println("a || b"+(a||b)); // 逻辑或运算中,两个变量有一个为真,则结果为真
       System.out.println("!(a && b)"+!(a&&b));// 如果是真,则为假。反之为真

       //短路运算
       int 5;
       boolean = (4) && (c++ 4); // False
       System.out.println(d);
       System.out.println(c);// c++ 后,c还是5
  }
}

 

位运算符:

根据“位”来运算

&: 00是0, 01是0, 11是1

|:有一个为1,就是1。

^: 取反 相同为0, 不相同为1

~: 完全相反

package operator;

public class Demo06 {
   public static void main(String[] args) {
       /*
       A = 0011 1100
       B = 0000 1101

      A&B 与 0 0 0 0 1 1 0 0
      A|B 或 0 0 1 1 1 1 0 1
      A^B 取反 0011 0001
      ~B 1111 0010
       */

  }
}

位运算符的面试题

2*8怎么算最快?

2 * 2 * 2 * 2

<< 左移 乘以2 *2

">>"右移 除以2 /2

效率奇高

 

扩展赋值运算符

+= b; // a = a + b 
-= b; // a = a - b 

细节:String出现在运算里,会把其他数字也转换成String类型进行拼接

System.out.println("" +a+b); // 结果为1020

package operator;

public class Demo07 {
   public static void main(String[] args) {
       int 10;
       int 20;

       += b; // a = a + b
       -= b; // a = a - b

       System.out.println(a);

       //字符串连接符 + ,
       // 出现了String类型, 会把其他的数字也转换成Stirng类型进行拼接
       System.out.println("" +a+b); // 1020 String在前面转换 发挥作用
       System.out.println(a+b+""); // 30

  }
}

 

条件运算符 ?:

x ? y : z 如果x==true 则结果为y 否则结果为z

package operator;
//三元运算符 ? :
public class Demo08 {
   public static void main(String[] args) {
       // x ? y : z
       // 如果x==true 则结果为y 否则结果为z

       int score 50;
       String type score 60 "not qualified" : "Qualified";
       System.out.println(type); // 50 < 60: not qualified
  }
}

//笔记来源于秦老师课程https://www.kuangstudy.com/course
posted @ 2021-02-05 16:56  NTE701  阅读(92)  评论(0)    收藏  举报
1