day3 各种运算符

2022-07-25,

开头鸣谢狂神大大,这是他的地址
https://www.bilibili.com/video/BV12J41137hu?p=31

基本运算符

算数运算符

正常加减乘除不说

# 特例 #
public class helloworld {
   static public void main(String[] args) {
       int a = 10;
       int b = 20;
       System.out.println((double) a/b);//这里正常是等于0.5,但是int不能显示小数,直接被等于0,
      //所以要强制转换为double再运算。
   } 其他正常加减乘除就好
}

↑这是算法中应避免的状况↑

取模

 		int c = 10;
        int e = 20;
        System.out.println(e%c);=0,这个叫取模,简单来说就是余
        3/2 =11. 2/2=10
        2%2=0 就是余0的意思

这是自增实例,自减反之。

public class helloworld {
    static public void main(String[] args) {
    int a = 3;   //初始值是3
    int b = a++;//在这里,b先被赋值成了=a,然后再+1,在这行a=4,b=3
    int c = ++a;//在这里,a先被+1,然后再执行 c=a,也就是a=c=5
        System.out.println(a);
        System.out.println(b);
        System.out.println(c); 答案是输出5,3,5
    } 自增自减是可以改变数据原本的数值的,每一次都相当于重新赋值。
}

自增自减会在运算过程中使变量被重新被赋值!!

幂运算 Math.pow(x,x)

double p =Math.pow(2,10); //新建一个变量p,引用Math.pow(这是一个工具)来计算2^10
       System.out.println(p);// =1024

位运算

public class helloworld {

   static public void main(String[] args) {
       //试试位运算
       /*
       A= 0011 1100
       B= 0000 1101
      ~B= 1111 0010 取反
     A&B= 0000 1100 与
     A|B= 0011 1101 或
     A^B= 0011 0001 异或 意思就是只要相等为0 不相等为1。
        好,问题来了,2*8怎么算最快,答案就是位运算。
        */
       int a = 2;//二进制是10
       int b = a << 2;//将10往左移2位变成1000
       //记住 如果像1000类似的二进制,那么他有几个0就是2的几次方。三个0,三次方等于8
       System.out.println(b);//输出结果为8
       double x = Math.pow(2,5);
       System.out.println(x);
        //简单来讲<<4相当于当前
       int c = a << 4;//将2也就是10左移4得到100000,这里有五个0等于2^5直接和上方Math类算式一样。
       System.out.println(c);
   }

}

条件运算符

public class helloworld {

   static public void main(String[] args) {
        int a = 11;
        int b = 10;
        String 当上程序员 = a>b ? "升职加薪": "当条咸鱼";
       System.out.println(当上程序员);
   }
}

输出升职加薪

扩展赋值运算符

       int a = 5;
       int b = 10;
       a+=b;// a=a+b
       System.out.println(a);输出结果为15
       把变量B的值加在变量A上,这改变了变量。
+= -= *= /=

上面四个差不多道理。

运算时的类型转换

public class helloworld {
    static public void main(String[] args) {
        byte a = 1;
        short b = 10;
        int c = 100;
        long d = 200;
        System.out.println(a+b+c+d);// =311 long
        取多个数据类型中最高类型作为返回值数据类型
        System.out.println(b+c+d);//= 310 int
        byteshortchar用运算符运算后自动转型为int类型
        System.out.println(c+d);//=300 int
    }
}

关系运算符

< > == >= <= !=

如上。
它的返回值是布尔值,通常配合 if来使用。

与或非

boolean a = true;
boolean b = false;
   System.out.println(a&&b);false 符号两边都为truetrue,否则反之
   System.out.println(a||b);true 符号两边只要有1truetrue,都为falsefalse
   System.out.println(!(a&&b));true a&&b输出false则为true,反之亦然。
    }
}

短路运算

int a = 5;
    boolean b = (a<4) && (++a==6); 当它算到a(5) < 4 这个值为假就不会继续往下。
		System.out.println(b); false
 		System.out.println(a);输出5
 		如果它接着算,那么a就会自增一次变为6,但它没有,这就是一次短路运算。
 		但如果把第一个算式改为或语法就会变成6

字符串连接

public class helloworld {

  static public void main(String[] args) {
       int a = 5;
       int b = 10;
       String c = "测试";
      System.out.println(a+b);
      System.out.println(c+a+b);
      System.out.println(a+b+c);
  }
输出结果
15
测试510
15测试
}

如上所示,原理像是前面的短路运算。

本文地址:https://blog.csdn.net/kirizz/article/details/112173092

《day3 各种运算符.doc》

下载本文的Word格式文档,以方便收藏与打印。