运算符

运算符:


Java支持如下运算符:

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

  • 赋值运算符: =

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

  • 逻辑运算符:&& || !

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

  • 条件运算符 : ?:

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


算数运算符:

package operator;

public class Domo01 {
   public static void main(String[] args) {
       int a = 10;
       int b = 20;
//     ctrl+d 赋值当前行到下一行

       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 =1231231213L;
       int b = 100;
       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;
       //判断关系 返回值为false或者true
       System.out.println(a==b);//false
       System.out.println(a<=b);//true
       System.out.println(a>=b);//false
       System.out.println(a!=b);//true
       //取余
       System.out.println(c%a);//输出1
  }
}

自增自减运算符

package operator;

public class Demo04 {
   public static void main(String[] args) {
       int a = 10;
       int b = 10;
       int c = a++;//执行完这段代码 先给b赋值 在自增
       int d = ++b;//执行完这段代码 先自增 再给b赋值

       System.out.println(a); //11
       System.out.println(b);  //11
       System.out.println(c); //10 先赋值 在自增
       System.out.println(d); //11   先自增 在赋值 所以为11

       //幂运算 2的3次方 使用工具类Math
       double e = Math.pow(2,3);
       System.out.println(e);

  }
}

 

posted @ 2021-05-24 10:35  Dudo1  阅读(60)  评论(0)    收藏  举报