运算符

 

运算符

 

 

 

算术运算符&赋值运算符

package operator;
public class Demo01 {
   public static void main(String[] args) {
       //二元运算符
       //Ctrl +D : 复制当前行到下一行
       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);
  }
}
package operator;

public class Demo02 {
   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
  }
}

 

关系运算符

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);

       System.out.println(a>b);
       System.out.println(a<b);
       System.out.println(a==b);
       System.out.println(a!=b);
  }
}

 

注:自增或自减运算

package operator;

import static java.lang.Math.pow;

public class Demo04 {
   public static void main(String[] args) {

       // ++ -- 自增 自减
       int a = 3;
       int b = a++;  //执行完这行代码后,先给b赋值,再自增
       System.out.println(a);
       int c = ++a;  //执行完这行代码前,先自增,再给c赋值
       System.out.println(a);
       System.out.println(b);
       System.out.println(c);

       //幂运算2^3 2*2*2 = 8   很多运算,我们会用一xie
       double pow = pow(3,2);
       System.out.println(pow);

  }
}

 

逻辑运算符

package operator;

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

       //短路运算
        int c = 5;
        boolean d = (c<4)&&(c++<4);
       System.out.println(d);
       System.out.println(c);//结果为5。 boolean d = (c<4)&&(c++<4)如果(c<4)false,后面(c++<4)将不执行,所以c=5.

  }
}

位运算符

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,其他为0
       A^B = 1100 0011   异或:相同为1,不同为0
       ~B = 1111 0010   取反:0为1,1为0

       <<左移 *2
       >>右移 /2
        */
  }

}

条件运算符

package operator;

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

       int scroe = 50;
       String type = scroe < 60 ? "不及格":"及格";//必须掌握
       System.out.println(type);

  }
}

 

拓展赋值运算符

package operator;

public class Demo07 {

   public static void main(String[] args) {
       int a = 10;
       int b = 20;

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

       //字符串连结符 + ,String
       System.out.println(a+b);
       System.out.println(""+a+b); //1020 当“+”两侧出现String,会将”+“两则转换成String类型,并进行连结
       System.out.println(a+b+"");//30 先进行了a+b运算,再转换成String型
  }
}

 

 

posted @ 2021-05-14 19:53  张生啊  阅读(43)  评论(0)    收藏  举报